0
votes

my current code is below.

What I have is two sets of data phi and theta both 18x30 and I have a for loop running from 1 to 30 which are the 30 columns of my data. Each of these individual columns will produce a matrix 'B' for me. The matrix 'B' is produced after going through the 18 rows of each column.

The problem I have is that I need to multiply all the resulting 'B' matrices for each of the 18 rows with each other in order to get a final matrix for each of the 30 columns, that is why I have set A(:,:,i) = eye(2) so that my first matrix will multiply by A. But I don't want to store A or B for each loop, instead what I want is that on the first loop of i my matrix B will multiply the identity matrix A. Then that matrix A will multiply the next matrix B...with the result of each multiplication being carried forward to the next matrix B that will be calculated, so the multiplications will get done as the program loops. That's why I have the line:

A(:,:,i) = A.*B;

but it doesn't seem to work. I get an error message saying the dimensions must match.

At the end of the program I want to be able to access each of the 30 matrices using a command like: A(:,:,3), for example, to get my 2x2 matrix for the 3rd column.

Hope that was clear enough!

theta = dlmread('theta.dat');
phi = dlmread('phi.dat');

ne = 1.7;
no = 1.5;
d = 0.000001;
w = 0.000000555;

for i = 1:30
        A(:,:,i) = eye(2);

for j = 1:18    

    nx =((cos(theta(j,i)).^2)/(no^2) + ((sin(theta(j,i)).^2)/(ne^2))).^(-1/2);
    D = (2*pi*(nx-no)*d)/w;

    x = ((cos(phi(j,i))).^2).*exp((-1i.*D)/2) + ((sin(phi(j,i))).^2).*exp((1i.*D)/2);
    y = 1i*(sin(D/2)).*sin(2*phi(j,i));
    z = ((cos(phi(j,i))).^2).*exp((1i.*D/2) + ((sin(phi(j,i))).^2).*exp((-1i.*D)/2));

    B = [x y;y z];
    A(:,:,i) = A.*B;

end

end
1

1 Answers

1
votes

B is a 2x2 matrix. For A.*B to work, A must also be 2x2. But A in your program is three-dimensional.

From your problem description, I think you want

A(:,:,i) = A(:,:,i)*B;  % Edited now that I see this happens 18 times on the same i

(Please note, I also replaced element-wise multiply .* with matrix multiply *, because that's what it sounds like you want.)


But I suggest

A = eye(2);

and

A = A*B;

and store it at the end like

results(:,:,i) = A;