MATLAB Answers

0

How to iterate through a Matrix using for loops?

Asked by Michelle Wong on 17 Sep 2019
Latest activity Commented on by Stephen Cobeldick on 22 Sep 2019 at 6:32
This question was flagged by Walter Roberson

  2 Comments

More efficient
KG = zeros(24);
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;
for n = 1:21
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);
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];
for el = 1:21
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);
end
end
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

Sign in to comment.

2 Answers

Answer by Walter Roberson
on 17 Sep 2019

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 Comments

Sign in to comment.


Answer by Raj
on 17 Sep 2019
Edited by Raj
on 17 Sep 2019

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 Comments

My "More efficient" code posted in the comments does all these things.
However, none of them affect why the impression is given that only one theta value is used.
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 :)
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.

Sign in to comment.