필터 지우기
필터 지우기

How to iterate through a Matrix using for loops?

조회 수: 12 (최근 30일)
Michelle Wong
Michelle Wong 2019년 9월 17일
댓글: Rena Berman 2019년 10월 28일
Im currently trying to use a for loop to generate different 'Ke' matrices and storing it all in its distinct location in the KG matrix. However, my for loop currently only stops at the first index of theta (which is zero) and does not move on to other values in the 'theta' matrix. Any suggestions?
KG = zeros(24);
for n = 1:21
theta = [0, 0, 90, 45, 135, 30, 90, 0, 136.4, 15, 90, 165, 46.4, 0, 90, 45, 0, 90, 150, 135, 0];
theta = theta.*pi/180;
Ke = [cos(theta(n))^2 sin(theta(n))*cos(theta(n)) -cos(theta(n))^2 -sin(theta(n))*cos(theta(n));
sin(theta(n))*cos(theta(n)) sin(theta(n))^2 -sin(theta(n))*cos(theta(n)) -sin(theta(n))^2;
-cos(theta(n))^2 -sin(theta(n))*cos(theta(n)) cos(theta(n))^2 sin(theta(n))*cos(theta(n));
-sin(theta(n))*cos(theta(n)) -sin(theta(n))^2 sin(theta(n))*cos(theta(n)) sin(theta(n))^2]*k_stiff(n);
n = n + 1;
for el = 1:21
iel = [1, 2, 2, 1, 3, 11, 3, 3, 4, 10, 4, 9, 4, 4, 5, 5, 5, 6, 8, 7, 6];
jel = [2, 3, 12, 12, 12, 12, 11, 4, 11, 11, 10, 10, 9, 5, 9, 8, 6, 8, 9, 8, 7];
i = iel(el);
j = jel(el);
KG(2*i-1, 2*i-1) = Ke(1,1);
KG(2*i-1, 2*i) = Ke(1,2);
KG(2*i-1, 2*j-1) = Ke(1,3);
KG(2*i-1, 2*j) = Ke(1,4);
KG(2*i, 2*i-1) = Ke(2,1);
KG(2*i, 2*i) = Ke(2,2);
KG(2*i, 2*j-1) = Ke(2,3);
KG(2*i, 2*j) = Ke(2,4);
KG(2*j-1, 2*i-1) = Ke(3,1);
KG(2*j-1, 2*i) = Ke(3,2);
KG(2*j-1, 2*j-1) = Ke(3,3);
KG(2*j-1, 2*j) = Ke(3,4);
KG(2*j, 2*i-1) = Ke(4,1);
KG(2*j, 2*i) = Ke(4,2);
KG(2*j, 2*j-1) = Ke(4,3);
KG(2*j, 2*j) = Ke(4,4);
el = el + 1;
end
end
  댓글 수: 3
Stephen23
Stephen23 2019년 9월 22일
Michelle Wong 's original question (from Google Cache):
Im currently trying to use a for loop to generate different 'Ke' matrices and storing it all in its distinct location in the KG matrix. However, my for loop currently only stops at the first index of theta (which is zero) and does not move on to other values in the 'theta' matrix. Any suggestions?
KG = zeros(24);
for n = 1:21
theta = [0, 0, 90, 45, 135, 30, 90, 0, 136.4, 15, 90, 165, 46.4, 0, 90, 45, 0, 90, 150, 135, 0];
theta = theta.*pi/180;
Ke = [cos(theta(n))^2 sin(theta(n))*cos(theta(n)) -cos(theta(n))^2 -sin(theta(n))*cos(theta(n));
sin(theta(n))*cos(theta(n)) sin(theta(n))^2 -sin(theta(n))*cos(theta(n)) -sin(theta(n))^2;
-cos(theta(n))^2 -sin(theta(n))*cos(theta(n)) cos(theta(n))^2 sin(theta(n))*cos(theta(n));
-sin(theta(n))*cos(theta(n)) -sin(theta(n))^2 sin(theta(n))*cos(theta(n)) sin(theta(n))^2]*k_stiff(n);
n = n + 1;
for el = 1:21
iel = [1, 2, 2, 1, 3, 11, 3, 3, 4, 10, 4, 9, 4, 4, 5, 5, 5, 6, 8, 7, 6];
jel = [2, 3, 12, 12, 12, 12, 11, 4, 11, 11, 10, 10, 9, 5, 9, 8, 6, 8, 9, 8, 7];
i = iel(el);
j = jel(el);
KG(2*i-1, 2*i-1) = Ke(1,1);
KG(2*i-1, 2*i) = Ke(1,2);
KG(2*i-1, 2*j-1) = Ke(1,3);
KG(2*i-1, 2*j) = Ke(1,4);
KG(2*i, 2*i-1) = Ke(2,1);
KG(2*i, 2*i) = Ke(2,2);
KG(2*i, 2*j-1) = Ke(2,3);
KG(2*i, 2*j) = Ke(2,4);
KG(2*j-1, 2*i-1) = Ke(3,1);
KG(2*j-1, 2*i) = Ke(3,2);
KG(2*j-1, 2*j-1) = Ke(3,3);
KG(2*j-1, 2*j) = Ke(3,4);
KG(2*j, 2*i-1) = Ke(4,1);
KG(2*j, 2*i) = Ke(4,2);
KG(2*j, 2*j-1) = Ke(4,3);
KG(2*j, 2*j) = Ke(4,4);
el = el + 1;
end
end
Rena Berman
Rena Berman 2019년 10월 28일
(Answers Dev) Restored edit

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2019년 9월 17일
The code does go through all of the different theta values. However, your for el loop writes to the same places in KG for each different theta value, so the last iteration overwrites all of the previous ones.

Raj
Raj 2019년 9월 17일
편집: Raj 2019년 9월 17일
Shift these lines of code outside the 'for' loop:
theta = [0, 0, 90, 45, 135, 30, 90, 0, 136.4, 15, 90, 165, 46.4, 0, 90, 45, 0, 90, 150, 135, 0];
theta = theta.*pi/180;
That'll work. Also you can shift these lines as well (outside the loops):
iel = [1, 2, 2, 1, 3, 11, 3, 3, 4, 10, 4, 9, 4, 4, 5, 5, 5, 6, 8, 7, 6];
jel = [2, 3, 12, 12, 12, 12, 11, 4, 11, 11, 10, 10, 9, 5, 9, 8, 6, 8, 9, 8, 7];
There is no need of
n=n+1;
and
el=el+1;
The 'for' loop takes care of increments automatically.
  댓글 수: 3
Raj
Raj 2019년 9월 17일
편집: Raj 2019년 9월 17일
Yeah right....my mistake. Thanks for pointing out walter sir. I wrote all my 'comments' on the code but forgot to address the main issue raised in the question. I missed this part in the first answer:
"my for loop currently only stops at the first index of theta (which is zero) and does not move on to other values in the 'theta' matrix" - The value of KG you are getting is not from the first index of theta but the last index value of theta which in this case is zero (same as first value) and hence you are getting confused. For each value of Ke, you get one KG matrix. So the first loop is indeed running all values of theta but the second loop is overwriting KG with each value of Ke.
@walter sir: You missed to give any suggestion that'll help solving the issue. My recommendation is to store all the KG matrices in a cell array. Please suggest any better alternative if you have any in mind. Thanks again :)
Walter Roberson
Walter Roberson 2019년 9월 17일
Not enough information about the desired size and class of output. It is not clear to me that creating anything other than 24 x 24 is desired, especially with n=1:21 and el=1:21 both being the same size. Perhaps the intention would be met by not using for el and instead indexing those things by n. Or perhaps assign to KG(2*i-1, 2*i-1, n) and so on to create a 24 x 24 x 21 array.

댓글을 달려면 로그인하십시오.

카테고리

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