Effacer les filtres
Effacer les filtres

Loop help

1 vue (au cours des 30 derniers jours)
Syed Abbas
Syed Abbas le 5 Jan 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.

Réponse acceptée

Sean de Wolski
Sean de Wolski le 5 Jan 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 commentaire
Syed Abbas
Syed Abbas le 5 Jan 2012
Thanks - much simpler

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

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by