How I can save vectors in for loops
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Star Strider
2015-5-3
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 个评论
Star Strider
2015-5-3
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.)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!