# Sort and Save Matrices Eigenvectors to a Larger Matrix

We are going to sort and save all orthoginalized-eigenvectors of 4*4 random matrices (produced in any iteration) to a larger matrix such as storagematrix.

Since, produced random matrices are 4*4, and between 0 and 2, there are 11 numbers with 0.2 intervals, we must have a 11*4 storage matrix to save all sorted eigenvectors (corresponded to orthoevecs in the bellow code)

nx = 2; intervals = 0.2; diprandom = 4; j=0;
storagematrix = ConstantArray[0, {diprandom*(Round[nx/intervals] + 1), diprandom}];

Do[
testmatrix = (2 - r)*RandomInteger[{-5, 5}, {diprandom, diprandom}];

{evalvs, evecs} = Eigensystem[testmatrix];
{evalvs, orthoevecs} = {evalvs[[#]], Orthogonalize[evecs[[#]]]} &@ Ordering[evalvs];

Do[
storagematrix[[(i + j) ;;]] = orthoevecs[[i]];
,{i, 1, 4}];
j+=4;
,{r, 0, nx, intervals}];


But the problem which I face is in bellow picture (which is related to storage matrix as storagematrix // N // MatrixForm)

for r=1.98 and r=2. For r=2, testmatrix is a zero matrix and must have eigenvectors: {1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}. But I could not see {1,0,0,0}

• What does "doesn't work" mean? What errors do you get? Jul 25, 2015 at 8:37
• I mean I face to this message: Single or list of non-negative machine-sized integers expected at position 2 of ConstantArray[0,{11.,4}]. >> Jul 25, 2015 at 8:45
• That's a rather informative error, isn't it? 11. is a Real, not an integer. If you make interval an exact number, at least that problem should go away. Jul 25, 2015 at 8:49
• You are right but we must multiply by another factor: {diprandom*(Round[nx/intervals] + 1) Jul 25, 2015 at 9:15
• Also we must replace j+=1 with j+=4, but we face to a big problem yet which I write in the main question Jul 25, 2015 at 9:39

The problem is with the behaviour of Set. Consider the following example:

a = {1,2,3,4,5};
a[[2;;]] = {1,2,3,4}
a[[3;;]] = {1,2,3,4}
a[[4;;]] = {1,2,3,4}
a[[5;;]] = {1,2,3,4}


Notice that in the [[2;;]] part, Mathematica decides you want to replace elements 2, 3, 4 and 5 of a with the corresponding elements 1, 2, 3, 4 rather than all with the list {1,2,3,4}. You can fix this by storagematrix[[(i + j)]] instead of storagematrix[[(i + j) ;;]].

In a more Mathematica-idiomatic style, by the way, your program would (I think) be (up to some simple reordering of the eigenvectors which I don't have time to check now):

Flatten[Last /@
SortBy[Transpose@
MapAt[Orthogonalize,
Eigensystem[(2-#) RandomInteger[{-5, 5}, {4, 4}]],
2],
First] & /@
Range[0, 2, 0.2],
1]


This is quite a bit faster for larger inputs.

To match your input more exactly:

Block[{nx=2, intervals=0.2, diprandom=4},
Flatten[Last /@
SortBy[Transpose@
MapAt[Orthogonalize,
Eigensystem[(nx-#) RandomInteger[{-5, 5}, {diprandom, diprandom}]],
2],
First] & /@
Range[0, nx, intervals],
1]
]

• your code is shorter and faster mine. It works very well, Thanks a bunch for that. I named the result (before Flatten...) "stored=". If we write this code larglist={};Do[ Do[ If[Norm[sorted[[i]].sorted[[i + n]]] < 0.5, AppendTo[largelist, Norm[sorted[[i]].sorted[[i + n]]]]] , {n, 4, 7}]; , {i, 1, 36, 4}].... in order to multiply each eigenvector of every number in _Range_ to the each eigenvector of next number in the _Range_, and a comparison with 0.5 and save in larglist this code works, but how can I shorten this one, because it seems you are professional OP. Jul 25, 2015 at 11:33
• @Ackaran, you have some typos (largelist instead of larglist, and the i bound should probably go up to 37). Norm /@ Flatten[ First[#1].Transpose[#2] & @@@ Partition[Partition[storagematrix, 4], 2, 1]] // Select[# < 0.5 &]` However, you should probably post this as a new question or an amendment to your current question - I'm not sure what official policy is. It's worth getting used to the list manipulation functions. (I'm not professional, by the way, but a student.) Jul 25, 2015 at 15:09
• Not only you are professional to Math but also your so intelligent, You are right. 37 and largelist are completely correct. Jul 25, 2015 at 15:19