How to save each resultant matrix from a for loop

I want to pick each resultant value of K from the first loop and insert into the second loop one by one
clc
clear all
no_nodes = 5;
A = 8;
E = 1.9e6;
L = 36;
k = A*E/L;
th = [0,pi/4,pi/2,3/4*pi];
for i = 1:4
theta = th(i);
s = sin(theta);
c = cos(theta);
angle_mat =[c.^2 s*c -c.^2 -s*c;
s*c s.^2 -s*c -s.^2;
-c.^2 -s*c c.^2 s*c ;
-s*c s.^2 s*c s.^2 ];
K = k*angle_mat;
end
zero_mat = zeros(no_nodes*2);
for i = [1 3 5 7]
zero_mat(i:i+3,i:i+3)= K
zero_mat = zeros(no_nodes*2);
end
zero_mat = 10×10
1.0e+05 * 2.1111 -2.1111 -2.1111 2.1111 0 0 0 0 0 0 -2.1111 2.1111 2.1111 -2.1111 0 0 0 0 0 0 -2.1111 2.1111 2.1111 -2.1111 0 0 0 0 0 0 2.1111 2.1111 -2.1111 2.1111 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
zero_mat = 10×10
1.0e+05 * 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2.1111 -2.1111 -2.1111 2.1111 0 0 0 0 0 0 -2.1111 2.1111 2.1111 -2.1111 0 0 0 0 0 0 -2.1111 2.1111 2.1111 -2.1111 0 0 0 0 0 0 2.1111 2.1111 -2.1111 2.1111 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
zero_mat = 10×10
1.0e+05 * 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2.1111 -2.1111 -2.1111 2.1111 0 0 0 0 0 0 -2.1111 2.1111 2.1111 -2.1111 0 0 0 0 0 0 -2.1111 2.1111 2.1111 -2.1111 0 0 0 0 0 0 2.1111 2.1111 -2.1111 2.1111 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
zero_mat = 10×10
1.0e+05 * 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2.1111 -2.1111 -2.1111 2.1111 0 0 0 0 0 0 -2.1111 2.1111 2.1111 -2.1111 0 0 0 0 0 0 -2.1111 2.1111 2.1111 -2.1111 0 0 0 0 0 0 2.1111 2.1111 -2.1111 2.1111
as it can be seen that at the moment it is only picking the last resultant matrix and putting it in the second loop

 Accepted Answer

Matt J
Matt J on 30 May 2022
Edited: Matt J on 30 May 2022
One way
no_nodes = 5;
A = 8;
E = 1.9e6;
L = 36;
k = A*E/L;
th = [0,pi/4,pi/2,3/4*pi];
K=cell(1,4);
for i = 1:4
theta = th(i);
s = sin(theta);
c = cos(theta);
angle_mat =[c.^2 s*c -c.^2 -s*c;
s*c s.^2 -s*c -s.^2;
-c.^2 -s*c c.^2 s*c ;
-s*c s.^2 s*c s.^2 ];
K{i} = k*angle_mat;
end
zero_mat = zeros(no_nodes*2);
for j = 1:4
i=2*j-1;
zero_mat(i:i+3,i:i+3)= K{j}
zero_mat = zeros(no_nodes*2);
end

5 Comments

Thank you very much for the response. I was stuck on this for last week.
I understand you must be busy but can you please briefly explain the changes that you made?
We are now saving the matrices K to a cell array,
K{i} = k*angle_mat;
from which we can easily recover them in the second loop.
Sorry, I forgot to mention that The resultant matrices from the second loop is required to be saved individually like the first one. I applied the same method for the second loop but it says that it doesnt support brace indices.
clc;
clear all;
no_nodes = 5;
A = 8;
E = 1.9e6;
L = 36;
k = A*E/L;
th = [0,pi/4,pi/2,3/4*pi];
K=cell(1,4);
for i = 1:4
theta = th(i);
s = sin(theta);
c = cos(theta);
angle_mat =[c.^2 s*c -c.^2 -s*c;
s*c s.^2 -s*c -s.^2;
-c.^2 -s*c c.^2 s*c ;
-s*c s.^2 s*c s.^2 ];
K{i} = k*angle_mat;
end
zero_mat = cell(1,4)
for j = 1:4
i=2*j-1;
zero_mat = zeros(no_nodes*2);
zero_mat(i:i+3,i:i+3)= K{j}
zero_mat{j}=zero_mat
end
You could do,
zero_mat = zeros(no_nodes*2, no_nodes*2,4);
for j = 1:4
i=2*j-1;
zero_mat(i:i+3,i:i+3,j)= K{j};
end
Thank you very much good sir!

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Network Parameters in Help Center and File Exchange

Products

Release

R2021a

Tags

Asked:

on 30 May 2022

Commented:

on 30 May 2022

Community Treasure Hunt

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

Start Hunting!