issue with saving matrix in for loop
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I am creating a big matrix 'U' in the for loop as shown below. After this I have to avergae this matrix.
As long as, if my 'kk' is contonous i.e. from 1 to 10000, i dont have any issue. But, for sometimes, my 'kk' is discontinous, for e.g. 5, 7, 11, 18, 23, here logically I should get U matrix of a x b x 5, beacuse I have read only 5 files. However, I end up with a x b x 23, this changes my average value. Is there any way to get rid of this ??...
for kk = 1:1:10000
U(:,:,kk) = fu2;
end
0 个评论
采纳的回答
David Durschlag
2021-1-29
The index being assigned to in your code is based on the loop variable (kk). If you assign to index 23, you will have a matrix with dimension at least 23, though it may be mostly empty.
By adding a second variable tracking your matrix index, you can reduce this dimension:
U = [];
index = 1
for kk = 1:3:10000
U(:,:,index) = fu2;
index = index + 1;
end
In this example code, I simulate your discontinuous kk by adding 3 each time instead of 1. The result is that U has dimension 3334 instead of 10000.
更多回答(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!