writing out data within a for loop
조회 수: 3 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
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.
추가 답변 (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!