I want to store the value of matrix in each iteration and add it to the stored matrix in the next iteration. At the end I would get the sum of all matrices in the loop.

4 次查看(过去 30 天)
Q=[ 171.1 3.622 0; 3.622 12.07 0 ; 0 0 4.5]
s=input('Total number of Layers:')
for n=1:s
x = input ('Enter the value of angle for the layer in Degrees:')
c=cos(degtorad(x));
s=sin(degtorad(x));
thetha=[c^2 s^2 -2*s*c; s^2 c^2 2*c*s; c*s -c*s c^2-s^2];
psi=[c^2 s^2 -s*c; s^2 c^2 c*s; 2*c*s -2*c*s c^2-s^2];
qbar=thetha*Q*inv(psi)
y=n;
disp(y)
A=qbar*input('Thickness of each layer:')*input('Number of layers:')
end
This is my code and I want to save the sum of A of all iterations in a separate variable (suppose B). How do I do that

回答(2 个)

Andrei Bobrov
Andrei Bobrov 2016-2-6
编辑:Andrei Bobrov 2016-2-6
Q = [ 171.1 3.622 0; 3.622 12.07 0 ; 0 0 4.5];
s0 = input('Total number of Layers:');
A = zeros(3,3,s0);
for n=1:s0
x = input ('Enter the value of angle for the layer in Degrees:');
x = degtorad(x);
c = cos(x);
s = sin(x);
c2 = c^2;
s2 = s^2;
cs = c*s;
cs2 = 2*cs;
cs22 = c2-s2;
thetha = [c2 s2 -cs2; s2 c2 cs2; cs -cs cs22];
psi = [c2 s2 -cs; s2 c2 cs; cs2 -cs2 cs22];
qbar = thetha*Q/psi;
disp(n);
A(:,:,n) = qbar*input('Thickness of each layer:')*input('Number of layers:');
end
B = sum(A,3);

Walter Roberson
Walter Roberson 2016-2-6
Before the loop,
B = 0;
At the end of the loop, after calculating A but before the "end", add
B = B + A;

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by