Loop help

4 visualizzazioni (ultimi 30 giorni)
Syed Abbas
Syed Abbas il 5 Gen 2012
hi, I have two columns of data. Column 1 just contains the numbers 1, 2 and 3 in no particular order and column 2 contains data points that correspond to the numbers 1,2 and 3 in column one. The data is arranged in ascending order by column one. I want to write a simple loop that groups the 1's together and calculates the mean of the corresponding values in column 2 and does the same same for 2's and the 3's. I just need some help in writing this loop. Thanks.

Risposta accettata

Sean de Wolski
Sean de Wolski il 5 Gen 2012
No reason for a for-loop:
X = [ceil(rand(10,1)*3) (1:10).']; %two column matrix of 1:3 in first column and values in second
group_means = accumarray(X(:,1),X(:,2),[],@mean); %mean grouped by first column
And of course:
doc accumarray
  1 Commento
Syed Abbas
Syed Abbas il 5 Gen 2012
Thanks - much simpler

Accedi per commentare.

Più risposte (0)

Categorie

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

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by