3
$\begingroup$

I am looking for a convenient way to generate a matrix of the following symmetric form (numbers could also run from -3 to +3 or just from -1 to +1):

enter image description here

Tips, anybody?

$\endgroup$
  • 2
    $\begingroup$ Array[{{-2, -1}, {-2, -1}} + {#1 - 1, #2 - 1} &, {4, 4}] $\endgroup$ – andre314 Jun 15 '14 at 17:20
4
$\begingroup$

One way:

n = 2;
Table[{{i, i + 1}, {j, j + 1}}, {i, -n, n - 1}, {j, -n, n - 1}]

or another

n = 5
Partition[Tuples[Partition[Range[-n, n], 2, 1], 2], 2 n] // MatrixForm

enter image description here

$\endgroup$
  • $\begingroup$ Thanks, your second solution is exactly what I needed. Would you accept my acceptance? $\endgroup$ – eldo Jun 15 '14 at 18:18
  • $\begingroup$ @eldo Thank :) I will glady accept it :P $\endgroup$ – Kuba Jun 15 '14 at 18:55
3
$\begingroup$
n = 2;
Table[{{-n, -n + 1} + i, {-n, -n + 1} + j}, {i, 0, 2 n - 1}, {j, 0, 
  2 n - 1}]
$\endgroup$
  • $\begingroup$ thanks, very nice, but it doesn't hold (in my case) for n>2. Try it with n=5 and compare with Kuba's answer. $\endgroup$ – eldo Jun 15 '14 at 18:30
  • $\begingroup$ @eldo I have changed it. I think it is working now fro any n. $\endgroup$ – Algohi Jun 15 '14 at 18:40
  • $\begingroup$ yes, now it works :) $\endgroup$ – eldo Jun 15 '14 at 19:02
1
$\begingroup$
Table[{{-2, -1}, {-2, -1}} +
   i*{{1, 1}, {0, 0}} +
   j*{{0, 0}, {1, 1}},
  {i, 0, 3}, {j, 0, 3}] //
 MatrixForm

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.