writing out data within a for loop

1 vue (au cours des 30 derniers jours)
Jenny
Jenny le 19 Fév 2014
Commenté : Jenny le 20 Fév 2014
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

Réponse acceptée

Iain
Iain le 19 Fév 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.
  1 commentaire
Jenny
Jenny le 20 Fév 2014
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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by