필터 지우기
필터 지우기

issue with saving matrix in for loop

조회 수: 1 (최근 30일)
Turbulence Analysis
Turbulence Analysis 2021년 1월 29일
댓글: Turbulence Analysis 2021년 1월 29일
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

채택된 답변

David Durschlag
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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by