hira - 6 months ago 44

R Question

I want to create symmetric circulant matrices.

Example of order 4:

1 2 : 3 4

2 1 : 4 3

.........

3 4 : 1 2

4 3 : 2 1

Example of order 8:

1 2 3 4 : 5 6 7 8

2 1 4 3 : 6 5 8 7

3 4 1 2 : 7 8 5 6

4 3 2 1 : 8 7 6 5

..................

5 6 7 8 : 1 2 3 4

6 5 8 7 : 2 1 4 3

7 8 5 6 : 3 4 1 2

8 7 6 5 : 4 3 2 1

How do I do this in R?

Answer

This appears to solve the problem but is way too clever. The `flip(x)==1`

idiom gives a matrix (once converted to numeric) of the form `[0 1; 1 0]`

...

```
flip <- function(x) x[rev(seq(nrow(x))),]
x <- matrix(c(1,2,2,1),2)
x2 <- kronecker(2*(flip(x)==1),x,"+") ## 4x4 solution
x3 <- kronecker(4*(flip(x)==1),x2,"+") ## 8x8 solution
```

Repeat for larger matrices of size `2^n`

(embed in a `for`

loop if you want to do this a lot) ... I don't know what your desired answer would be for a matrix that's not of size `2^n`

(e.g. 12x12), but you might be able to find a way to extend this machinery.