How to avoid overwriting inside a for loop?
23 次查看(过去 30 天)
显示 更早的评论
I'm using repmat inside my for loop. But at the end of the loop, it saves only the value of last iteration repeating that last value alone. How not to overwrite the values of repmat and extract all the values of iterations.
3 个评论
dpb
2019-7-25
frame=repmat(FR,1,size(centreDark(:,1)))
What do you think the purpose of this line is? What are you trying to do with it?
The loop does indeed not use the indexing variable FR to save anything from one iteration to the other so it does, indeed, overwrite all the variables you create each loop.
The question is, what do you intend/want?
回答(1 个)
Abhilash Padma
2019-7-29
Hi,
Instead of declaring “frame” as an empty array, declare it as an empty cell array.
frame={};
And inside for loop, assign the return value of repmat function to “frame{FR}”.Make the following changes in your for loop which may solve your problem.
frame{FR}=repmat(FR,1,size(centreDark(:,1)));
fr=frame{FR};
Now, the cell array “frame” stores every value of each iteration.
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!