How I can save vectors in for loops
8 views (last 30 days)
Show older comments
Dears, I want to know how can I save vectors in 3(or more) for loops My code is huge and I summarize it here.
for i=1:10
for j=1:15
for k=1:20
% some calculation
Vector1= something with size(1x2000) % size of Vector1 will be changed in each loop
end
end
end
finally I would like to attach all of Vector1 to one vector and capable to find the special array is related to which i and j and k.
0 Comments
Accepted Answer
Star Strider
on 3 May 2015
Since the length of the vector will change in each loop, I would use a cell array and a counter:
k1 = 0;
for i=1:10
for j=1:15
for k=1:20
% some calculation
k1 = k1 + 1;
Vector1{k1} = something with size(1x2000) % size of Vector1 will be changed in each loop
end
end
end
4 Comments
Star Strider
on 3 May 2015
My pleasure. Your explanation is not ‘bad’, sometimes it is not easy to describe what what we want our code to do.
If I understand you correctly:
1. The length of your vector (‘xc’ in my example) is determined by your computer’s memory. I do not know what you are calculating, so I cannot suggest a more efficient strategy.
2. In my example, i,j,k are stored as part of the cell element. If you want the cell array ‘Vector1’ to have 3 dimensions instead of 1, you can easily do that:
Vector1{i,j,k} = xc;
Then you can address each element individually. (You do not need the ‘k1’ counter if you do this.)
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!