writing out data within a for loop
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
Iain
on 19 Feb 2014
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.
More Answers (0)
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!