How to find a product of a 3 dimensional matrix over several iterations?

1 view (last 30 days)
Hello,
I need to code the product of matrix [A(i) B(i);C(i) D(i)] where i=1:N, N is an integer. Terms A,B,C and D of the matrix are of size 2x2x500. I've been struggling with forming the loop for days. Underneath is the loop I created although I dont think is taking the product of the matrix for i=1:N. any tips would be very much appreciated.
My code:
k=1:500:10000; %length of k
N=51; %number of iterations
for i=2:N
chain(1,1,:)=cos(k*Ln);
chain(1,2,:)=1j*((Z0(N-(i-1)))*sin(k*Ln));
chain(2,1,:)=1j*((1/Z0(N-(i-1)))*sin(k*Ln));
chain(1,2,:)=cos(k*Ln);
end

Answers (1)

Walter Roberson
Walter Roberson on 15 Jun 2013
Your setup code must not be correct.
Consider first i=2.
In the second asignment chain(1,2,:) is assigned and has to do with the scalar "i" (there would no point looping of none of the entries involved "i"). The assignment also has to do with the vector "k", which is a 20 element vector, so the right hand side produces a vector of length 20, that is assigned to chain(1,2,:); this makes chain to be at least 1 x 2 x 20. The next assignment, to chain(2,1,:) is likewise of length 20, so chain must be at least 2 x 2 x 20.
Let us assume for the moment that in your 4th assignment you meant the left-hand side to be chain(2,2,:) instead of overwriting chain(1,2,:) that you assigned 2 lines above.
Now, go on to the next value of "i". The second assignment, to chain(1,2,:), has to do with "i" (changed) and the vector k (unchanged), producing a vector of length 20. But look where it is being assigned -- to chain(1,2,1:20), which was already assigned to for the first "i" !
Examining further, the same problem applies to all four assignments, so the effect of the code is as if it had only been executed for i=N (the last value).
As you said "Terms A,B,C and D of the matrix are of size 2x2x500", even though they individually are not 2 x 2 by anything, we can speculate that what you want is for "chain" to end up 2 x 2 x 500. "k" is length 20, and you have 50 distinct N values, 2 to 51, and 20 * 50 = 1000, exactly twice as long as desired. So maybe you meant that "chain" should come out 2 x 2 x 1000 ?
If that is what you want, then assign to chain(1,1,:,i-1) and chain(1,2,:,i1) and so on, producing a 2 x 2 x 20 x 50 array. You can then reshape() that to be 2 x 2 x 1000 and do the multiplications based upon that.
I will not advise on the multiplication at this time. Algebraic matrix multiplication is order-dependent, so it is crucial that you specify the order the matrices are to be multiplied in, and whether the multiplications are left-multiplications or right-multiplications. Or perhaps you want element-by-element multiplication instead of matrix multiplication? Seems less likely to me as it appears you are constructing rotation matrices.
  1 Comment
catarina
catarina on 15 Jun 2013
Edited: Walter Roberson on 15 Jun 2013
Thanks for your explanation. Tried to do some changes to the code according to your suggestions and tried to perform the matrix multiplication. Some tips on what I done would be great! The 4th assignment which was chain(1,2,:) like the first assignment was a mistake and as you suggested I meant to put chain(2,2,:) and now changed them all to chain(1,1,:,i-1), chain(1,2,:,i-1) and so on. Now am trying to perform the multiplication of the matrices by performing basic matrix multiplication so for example the first row of the first matrix when i-1=1 is multiplied to the first column of the second matrix where i=2 and so on. So for example: For the first term of the multiplication of matrix(i-1=1)*matrix(i=2) we have: A(i-1=1)*A(i=2)+B(i-1=1)*C(i=2). Now I want the terms resultant of the first multiplication to be used to with i=3 and the result of this to i=4. I changed the code but am stuck and quite sure I'm doing something wrong. I hope I'm making myself clear, the changed code is underneath.
k=1:500:10000; %length of k
N=51; %number of iterations
for i=2:N
chain(1,1,:,i-1)=cos(k*Ln);
chain(1,2,:,i-1)=1j*((Z0(i))*sin(k*Ln));
chain(2,1,:,i-1)=1j*((1/Z0(i))*sin(k*Ln));
chain(2,2,:,i-1)=cos(k*Ln);
%assigning terms of matrices for i starting at 2
chain2(1,1,:,i)=cos(k*Ln);
chain2(1,2,:,i)=1j*((Z0(i))*sin(k*Ln));
chain2(2,1,:,i)=1j*((1/Z0(i))*sin(k*Ln));
chain2(2,2,:,i)=cos(k*Ln);
%deriving terms of matrices with basic matrix multiplication for matrix(i-1)*matrix(i)
A(:,:,:,i-1)= ((chain(1,1,:,i-1)).*(chain2(1,1,:,i)))...
+((chain(1,2,:,i-1)).*(chain2(2,1,:,i)));
B(:,:,:,i-1)= ((chain(1,1,:,i-1)).*(chain2(1,2,:,i)))...
+((chain(1,2,:,i-1)).*(chain2(2,2,:,i)));
C(:,:,:,i-1)= ((chain(2,1,:,i-1)).*(chain2(1,1,:,i)))...
+((chain(2,2,:,i-1)).*(chain2(2,1,:,i)));
D(:,:,:,i-1)= ((chain(2,1,:,i-1)).*(chain2(1,2,:,i)))...
+((chain(2,2,:,i-1)).*(chain2(2,2,:,i)));
%making the previous results to be used in the next iteration chain(1,1,:,i-1)= A(:,:,:,i-1);
chain(1,2,:,i-1)= B(:,:,:,i-1);
chain(2,1,:,i-1)= C(:,:,:,i-1);
chain(2,2,:,i-1)= D(:,:,:,i-1);
end

Sign in to comment.

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!