Azzera filtri
Azzera filtri

writing out data within a for loop

3 visualizzazioni (ultimi 30 giorni)
Jenny
Jenny il 19 Feb 2014
Commentato: Jenny il 20 Feb 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

Risposta accettata

Iain
Iain il 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.
  1 Commento
Jenny
Jenny il 20 Feb 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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by