How to iterate through a Matrix using for loops?
25 次查看(过去 30 天)
显示 更早的评论
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
2019-9-22
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
回答(2 个)
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.
0 个评论
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
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
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 Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!