how to stop overwriting a matrix in a for loop in matlab
    2 views (last 30 days)
  
       Show older comments
    
Hello all,
i'm stuck with this code.
I am trying to get the value of M. 
M is the product of three matrices, iterated and multiplied 100 times. please see below.
    for n  =1:100
        y = OUT.lin.matin(:,:,n);               %gets valiue from a stored array
        %y is a unitary matrix
           x = OUT.lin.db0(n);                      %gets valiue from a stored array
          % x is a scalar
        w =[exp(1i*x),exp(1i*-x)];        % creating the exponential 
        k = diag(w);                                     % creating a diagonal matrix of W
        M = y*k*y'; 
end
I think M overrides each iteration, but i would like to get the results from the 100 iterations.  Kindly assist.
Thanks in advance.
0 Comments
Accepted Answer
  KSSV
      
      
 on 9 Feb 2021
        M = zeros([],[],100) ; % give exact row and column size 
    for n  =1:100
        y = OUT.lin.matin(:,:,n);               %gets valiue from a stored array
        %y is a unitary matrix
           x = OUT.lin.db0(n);                      %gets valiue from a stored array
          % x is a scalar
        w =[exp(1i*x),exp(1i*-x)];        % creating the exponential 
        k = diag(w);                                     % creating a diagonal matrix of W
        M(:,:,n) = y*k*y'; 
end
4 Comments
  KSSV
      
      
 on 9 Feb 2021
				If the RHS is of size 2X2 use:
M = zeros(2,2,100) ; % give exact row and column size 
More Answers (0)
See Also
Categories
				Find more on Creating and Concatenating Matrices in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
