필터 지우기
필터 지우기

writing out data within a for loop

조회 수: 3 (최근 30일)
Jenny
Jenny 2014년 2월 19일
댓글: Jenny 2014년 2월 20일
I am dividing data into sectors and taking the max, mean and count of that sector. I can do this manually but would like to loop it to make the programming more efficient. I have attached the two relevant files. Dir = direction data. Spd = speed data.
The direction data is used to create an index where where the max, mean and count should be taken for the speed data. The direction sectors are indicated by Idx_Dir.
I also want to preallocate the output matrix (MMCmatrix) but I have not done this correctly.
What I would like to end up with is a 24 x 3 matrix
with the max for each sector in col 1,
the mean for each sector in col 2
and the count for each sector in col 3.
How do I write the calculations out into a matrix within the loop ?
Idx_Dir = [0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255, 270, 285, 300, 315, 330, 345, 360]; % 1 x 25
MMCmatrix = cell(1,length(Idx_Dir)-1); % pre-allocate the matrix size
for m = 1:length(Idx_Dir)-1; % m = 1 to 24
n = m+1; % n = 2 to 25
LoopIdx = (Dir >= Idx_Dir(m) & (Dir < Idx_Dir(n))); % for m = 1 and n = 2, LoopdIdx = where Dir >= 0 and Dir < 15;
max(m) = max(Spd(LoopIdx));
mean(m) = mean(Spd(LoopIdx));
count(m) = sum(LoopIdx);
MMCmatrix{m} = [max mean count];
end

채택된 답변

Iain
Iain 2014년 2월 19일
MMCmatrix{m} = [max mean count];
should be:
MMCmatrix(m,:) = [max mean count];
or
MMCmatrix(:,m) = [max mean count];
Depending if you want a 3xn or nx3 matrix.
  댓글 수: 1
Jenny
Jenny 2014년 2월 20일
Thank you. That solves how to write out the calculated data into the matrix however I still get the error:
Subscript indices must either be real positive integers or logicals.
when I run the loop.
The loop runs once, writes out the data, m = 2 and n =3. The next directional sector is determined for LoopIdx but the problem seems to be with determining the max and mean. I receive the error message at that stage and I do not understand why.
for m = 1:length(Idx_Dir)-1;
LoopIdx = (Dir >= Idx_Dir(m) & (Dir < Idx_Dir(n)));
max = max(Spd(LoopIdx));
mean = mean(Spd(LoopIdx));
count = sum(LoopIdx);
MMCmatrix(m,:) = [max mean count];
end

댓글을 달려면 로그인하십시오.

추가 답변 (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