"For" loop output storage

조회 수: 4 (최근 30일)
John Paul Donlon
John Paul Donlon 2012년 11월 7일
I have four sets of 10 points. I need to generate every possible coordinate system resulting from a combination of four points (one from each set). How can I use nested for loops to calculate and save each coordinate system? Here is what I have so far. Thank you in advance.
% The four sets of 10 points are stored in rows 1:10, with three columns per
% set (x, y, z), of the array 'array'
for a = 1:10
for b = 1:10
for c = 1:10
for d = 1:10
p1 = array(a,1:3);
p2 = array(b,4:6);
p3 = array(c,7:9);
p4 = array(d,10:12);
% Code to calculate coordinate system 'cs' defined by column
% vectors U, V, and W
cs = [U,V,W];
% This is where I need help: I need to save each 'cs' such that I
% have a list of coordinate systems; for example (r0, r1, r2,
% r3,...), where rN = [U,V,W]
end
end
end
end

채택된 답변

Jonathan Epperl
Jonathan Epperl 2012년 11월 7일
Use a cell array, that is not the most elegant or efficient way, but definitely the quickest fix:
Csystems = cell(10^4,1); % You'll have 10^4 different coord systems
for a = ...
% your code
cs = [U,V,W];
Csystems{1+ (10.^(0:3))*([d c b a]-1)'} = cs;
end
end
end
Now you can access your coordinate systems as
Csystems{1}
Csystems{2}
% ...
Csystems{10^4}
  댓글 수: 1
John Paul Donlon
John Paul Donlon 2012년 11월 7일
편집: John Paul Donlon 2012년 11월 7일
Thank you, Jonathan; that works.

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

추가 답변 (1개)

David Barry
David Barry 2012년 11월 7일
John, you could define a variable before the first for loop such as counter = 1; and then use this to index cs in the inner loop. Obviously don't forget to add 1 after each iteration.
  댓글 수: 1
John Paul Donlon
John Paul Donlon 2012년 11월 7일
David,
Thank you for your answer; how do I index 'cs'?
count = 1
...
cs(count) = [U,V,W];
count = count + 1;

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

카테고리

Help CenterFile Exchange에서 Simulink에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by