2
$\begingroup$

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.

Here is the matrix I want to create

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

3 Answers 3

5
$\begingroup$

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}]
$\endgroup$
3
  • $\begingroup$ 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? $\endgroup$
    – Quang Phan
    Jul 27, 2016 at 22:16
  • $\begingroup$ 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 *). $\endgroup$ Jul 28, 2016 at 0:09
  • $\begingroup$ For the numerical matrix, just run the third code I posted (matrix = Array[mat, {900, 900}]). $\endgroup$ Jul 28, 2016 at 0:15
5
$\begingroup$

Something like that?

The initial matrix (general case):

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

Gives: enter image description here

Then:

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

And finally:

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

Checking:

bigmat[[1]]

bigmat[[;; , 1]]

Diagonal[bigmat]
$\endgroup$
2
  • $\begingroup$ 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? $\endgroup$
    – Quang Phan
    Jul 27, 2016 at 23:09
  • $\begingroup$ @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. $\endgroup$ Jul 28, 2016 at 6:10
2
$\begingroup$

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

KroneckerProduct[Array[d, {30, 30}], Array[d, {30, 30}]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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