How can I Save data from for loop into an array

1 view (last 30 days)
AG
AG on 19 Mar 2022
Commented: AG on 19 Mar 2022
I have been trying to save the data generated in the T matrix for the six for loops in an array consist of 15,625 page where each page contains the 4x4 T matrix generated inside the six for loops. There are six for loops for six preceding variables each with a 5 numbers so the total result is 5^(6) solutions possible for the T matrix. Here I have attached the code I used to solve the matrix and is there anything I can do to increase the solving time, I sense the use of six for loops seems a bit obsolete. the code;
d4=10;
for d1=-15:8.75:20;
for d2=-12:6:12;
for d3=0:6:24;
for Theta4=-90:45:90;
for Theta5=-45:56.25:180;
for Theta6=-90:33.75:45;
T06=[sind(Theta4), cosd(Theta5), sind(Theta6), d1; cosd(Theta4), sind(Theta5), cosd(Theta6), d2; sind(Theta4), cosd(Theta5), sind(Theta6), d3+d4; 0, 0, 0, 1];
end
end
end
end
end
end
  2 Comments
Image Analyst
Image Analyst on 19 Mar 2022
T06 gets overwritten every iteration. How do you want to save it? To a 6-dimensional array? Or write the current T06 out to a text file with fprintf()?
AG
AG on 19 Mar 2022
thanks for your response but I want it to save each iteration individually, I tried to make an array such that I can call out a specific iteration in other words a specific solution for example the solution number 12300 I tried to do it as T06(:,:,12300) but it didn't work out for me. Therefore, if you could help me on that, I would be very thankful to you.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 19 Mar 2022
d4 = 10;
d1vals = -15:8.75:20;
d2vals = -12:6:12;
d3vals = 0:6:24;
Theta4vals = -90:45:90;
Theta5vals = -45:56.25:180;
Theta6vals = -90:33.75:45;
[vars{1:6}] = ndgrid(d1vals, d2vals, d3vals, Theta4vals, Theta5vals, Theta6vals);
vars = cellfun(@(C) reshape(C, 1, 1, []), vars, 'uniform', 0);
d1 = vars{1}; d2 = vars{2}; d3 = vars{3}; Theta4 = vars{4}; Theta5 = vars{5}; Theta6 = vars{6};
Z = zeros(size(Theta4));
ONE = ones(size(Theta4));
T06 = [sind(Theta4), cosd(Theta5), sind(Theta6), d1;
cosd(Theta4), sind(Theta5), cosd(Theta6), d2;
sind(Theta4), cosd(Theta5), sind(Theta6), d3+d4;
Z, Z, Z, ONE];
whos T06
Name Size Bytes Class Attributes T06 4x4x15625 2000000 double
  1 Comment
AG
AG on 19 Mar 2022
Thanks for your quick response. I am new to matlab and I want to know how to save the data just in the same way you did it but using the original code which uses a set of for loops. If you could help me on that, I would be very thankful to you.

Sign in to comment.

Products


Release

R2015a

Community Treasure Hunt

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

Start Hunting!