I want to create a N*N matrix A.
when n = 4
2 0 -2 0
2 0 2 0
0 2 0 -2
0 2 0 2
when n = 8
2 0 0 0 -2 0 0 0
2 0 0 0 2 0 0 0
0 2 0 0 0 -2 0 0
0 2 0 0 0 2 0 0
0 0 2 0 0 0 -2 0
0 0 2 0 0 0 2 0
0 0 0 2 0 0 0 -2
0 0 0 2 0 0 0 2
I can create this using nested for loop, but how to achieve it more efficiently? Are there any methods without for loop?
Thanks
diagfunction, this one can create off-diagonal elements as well.