How can I Save data from for loop into an array

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

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 2022년 3월 19일
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.

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

답변 (1개)

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

AG
AG 2022년 3월 19일
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.

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

카테고리

도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2015a

질문:

AG
2022년 3월 19일

댓글:

AG
2022년 3월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by