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}];

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

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

     storagematrix[[(i + j) ;;]] = orthoevecs[[i]];
   ,{i, 1, 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)

enter image description here

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}

  • $\begingroup$ What does "doesn't work" mean? What errors do you get? $\endgroup$ Jul 25, 2015 at 8:37
  • $\begingroup$ 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}]. >> $\endgroup$ Jul 25, 2015 at 8:45
  • 2
    $\begingroup$ 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. $\endgroup$ Jul 25, 2015 at 8:49
  • $\begingroup$ You are right but we must multiply by another factor: {diprandom*(Round[nx/intervals] + 1) $\endgroup$ Jul 25, 2015 at 9:15
  • $\begingroup$ Also we must replace j+=1 with j+=4, but we face to a big problem yet which I write in the main question $\endgroup$ Jul 25, 2015 at 9:39

1 Answer 1


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 /@ 
          Eigensystem[(2-#) RandomInteger[{-5, 5}, {4, 4}]],
   First] & /@
   Range[0, 2, 0.2],

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 /@ 
          Eigensystem[(nx-#) RandomInteger[{-5, 5}, {diprandom, diprandom}]],
   First] & /@
   Range[0, nx, intervals],
  • $\begingroup$ 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. $\endgroup$ Jul 25, 2015 at 11:33
  • 1
    $\begingroup$ @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.) $\endgroup$ Jul 25, 2015 at 15:09
  • $\begingroup$ Not only you are professional to Math but also your so intelligent, You are right. 37 and largelist are completely correct. $\endgroup$ Jul 25, 2015 at 15:19

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.