Loop help

3 views (last 30 days)
Syed Abbas
Syed Abbas on 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.

Accepted Answer

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

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!