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.

조회 수: 1 (최근 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