# Create matrix using loop?

Here I have a list of numbers (900 numerical values) denoted as d1-1, d1-2, d1-3.....d1-30, d2-1, d2-2, d2-3........d30-30. I want to create a matrix (900x900) which is organized as the below picture. Someone suggested to me that I should use the do loop or while loop. Can you guys help me with this? d1-2 has a magnitude the same as d2-1 but with negative sign.

• Surely, Vector multiplication is enough? – Feyre Jul 27 '16 at 21:52
• I don't recommend Subscripts as they cause problems when using Set, Information, etc. Try using brackets instead. E.g. d[1, 1] – JungHwan Min Jul 27 '16 at 22:08

Symbolic evaluation (if you only need some of the elements of the matrix, use this).

mat[row_, col_] :=
d[Ceiling[row/30], Ceiling[col/30]] d[Mod[row, 30, 1], Mod[col, 30, 1]]


For instance:

mat[50, 80]
(* d[2, 3] d[20, 20] *)


If you want the actual matrix, just do:

matrix = Array[mat, {900, 900}]


EDIT: If you must use a loop (which is slower), then try this:

matrix = Table[d[Ceiling[row/30], Ceiling[col/30]] d[Mod[row,
30, 1], Mod[col, 30, 1]], {row, 900}, {col, 900}]

• thank you, actually need a numerical matrix rather than symbolic matrix like this. I have the list of all the value for d[1,2], d[1,3]......up to d[30-30] (which is 900 numerical number). How can I assign those number to d[1,1], d[1,2].....in the above code? – Quang Phan Jul 27 '16 at 22:16
• If your list is level 2 ({{d[1, 1], ... d[1, 30]}, {d[2, 1], ... d[2, 30]}, ... {d[30, 1], ... d[30, 30]}}) then you could do Evaluate[Array[d, {30, 30}]] = (* your list *). If your list is level 1 ({d[1, 1], d[1, 2], ... d[30, 30]}), then try Evaluate[Flatten@Array[d, {30, 30}]] = (* your list *). – JungHwan Min Jul 28 '16 at 0:09
• For the numerical matrix, just run the third code I posted (matrix = Array[mat, {900, 900}]). – JungHwan Min Jul 28 '16 at 0:15

Something like that?

The initial matrix (general case):

mat = Array[d, {30, 30}]; mat // MatrixForm


Gives:

Then:

vec = Flatten[mat]; {vec} // MatrixForm


And finally:

bigmat = Transpose[{vec}].{vec};


Checking:

bigmat[[1]]

bigmat[[;; , 1]]

Diagonal[bigmat]

• Hi, thank you, however if you try to check bigmat[[1,2]] is give d[1,1]*d[1,2]=bigmat[[2,1]]. However as I said, d[1,2]=-d[2,1], do you have any idea how to fix it. Furthermore I have the list of all the value for d[1,2], d[1,3]......up to d[30-30] (which is 900 numerical number). How can I assign those number to d[1,1], d[1,2].....in the above code? – Quang Phan Jul 27 '16 at 23:09
• @Quang Phan. The input example I gave was the generic case, but you can always put d[j,i]=-d[i,j] in the example. Anyway, your own list of data (assuming it is a one dimension list of 900 elements) is just the intermediate "vec" entity I used, so I guess you just use Transpose[{vec}].{vec} to get the result you want. – Christian Néel Jul 28 '16 at 6:10

You don't need a For[] loop. What you need is KroneckerProduct[]:

KroneckerProduct[Array[d, {30, 30}], Array[d, {30, 30}]]