Loop help

4 visualizaciones (últimos 30 días)
Syed Abbas
Syed Abbas el 5 de En. de 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.

Respuesta aceptada

Sean de Wolski
Sean de Wolski el 5 de En. de 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 comentario
Syed Abbas
Syed Abbas el 5 de En. de 2012
Thanks - much simpler

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by