# How to average a column based on another column?

17 vues (au cours des 30 derniers jours)
Leon le 20 Fév 2014
Commenté : Leon le 20 Fév 2014
For example, I have a two column data as below:
1945 23.5
1952 25.6
1952 25.9
1952 23.5
1957 26.7
1957 31.2
How do I get a two column data as below, with the left column being the unique numbers: 1945, 1952, 1957, and the second column being the average values of that year:
1945 23.5
1952 25.0
1957 28.95
I can write a loop to do this, but think it must have an easier way to do this.
Thank you@
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Matt Kindig le 20 Fév 2014
This is a good use of accumarray():
[ud,ix,iy]=unique( data(:,1));
output = [ud, accumarray(iy,data(:,2),[],@mean)];
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Leon le 20 Fév 2014
So cool!
Thank you very much.

Connectez-vous pour commenter.

### Catégories

En savoir plus sur NaNs dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by