Filter löschen
Filter löschen

Loop help

1 Ansicht (letzte 30 Tage)
Syed Abbas
Syed Abbas am 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.

Akzeptierte Antwort

Sean de Wolski
Sean de Wolski am 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 Kommentar
Syed Abbas
Syed Abbas am 5 Jan. 2012
Thanks - much simpler

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by