-1

I have a matrix A and a vector u. I want to run a for loop to update the vector u. The operation is easy, I will multiple A by u (result is another vector denoted by z). Pick the largest element in z (result is scalar denoted by m). Update u by dividing the vector z with m. I don't know how to index the matrix so I used cell notation but that might not be correct.

 A=[1 2 3 4; 5 6 7 8 ; 9 10 11 12; 13 14 15 16];
 u= ones (4,1);


 for s=1:10
     z{s}= A*u{s};
     m(s)= max(z{s});
     u{s} = z{s}/m(s);
end

Any suggestions would be very helpful.

Henry
  • 171
  • 1
  • 11
  • 2
    Your code doesn't even run – it produces an error on the `z{s}= A*u{s}` line: "Cell contents reference from a non-cell array object." Always indicate error message in full. `u` is a vector, not a cell array, so you can't use cell array notation for it. It also only has four elements so indexing in to it when `s` is `5` will also produce an error. – horchler Aug 19 '15 at 15:56
  • I think you misinterpreted by question, I want to update u every time the loop is executed. the length of s is independent of size of u – Henry Aug 19 '15 at 15:58
  • The first time you update `u` you make it a `3x1` vector. It won't multiply `A` anymore. – beaker Aug 19 '15 at 16:02
  • I had typo, the matrix should be square.. thanks for your comment – Henry Aug 19 '15 at 16:05
  • @Henry Still, same error. `u{s}` is causing it – Luis Mendo Aug 19 '15 at 16:06
  • Okay, just remove all of the subscripts and it should work. – beaker Aug 19 '15 at 16:07
  • @LuisMendo, which error are you referring to? z and u are both of dimesion 4 by 1 and A is 4 by 4 matrix. – Henry Aug 19 '15 at 16:07
  • @Henry - You are basically implementing the Power Method to find the largest eigenvalue and associated eigenvector in a matrix. I had to think about your method for a minute and it suddenly hit me. Please consult the duplicate question for better insight on how to implement it. However, there are two answers below that illustrate your error and how to get it working properly, but I recommend reading the duplicate so that you can get this method working more robustly. – rayryeng Aug 19 '15 at 19:06
  • thanks for your note, that is correct that is what I am trying to do. Do you see any errors in the algorithm itself. For example assume that the elements of A are complex, how do I pick m is it the one with the largest magnitude @rayryeng ? – Henry Aug 19 '15 at 20:18
  • @Henry - No there aren't any errors at all, save the cell array indexing. However, if `A` has complex eigenvalues, then the Power Method will fail as it is not designed to handle complex eigenvalues. The matrix must have real eigenvalues for the Power Method to work. Read the first two pages of this document: http://www.math.ohiou.edu/courses/math3600/lecture16.pdf – rayryeng Aug 19 '15 at 20:20

1 Answers1

0

You don't have to index anything. Just remove all those indices. You should read the docs to learn when to index and when not to. What you have is just matrix operations.

 A=[1 2 3 4; 5 6 7 8 ; 9 10 11 12; 13 14 15 16];
 u= ones (4,1);

 for s=1:10
     z = A*u;
     m = max(z);
     u = z/m;
 end

The final value for u is

0.2028
0.4685
0.7343
1.0000
dpmcmlxxvi
  • 1,292
  • 1
  • 9
  • 13