find the same values and average it out

1 Ansicht (letzte 30 Tage)
Duncan
Duncan am 19 Aug. 2014
Beantwortet: Azzi Abdelmalek am 19 Aug. 2014
I have the following matrix A:
A =
66 2
66 3
66 4
67 6
67 3
68 5
69 6
69 3
what I would like to do is find the values that are the same in the first column and average the values in the second column and put it into a new matrix (B).
for example: I look for "66" and average the corresponding values in the second columns (i.e. 2,3,4) and the the mean is 3. So the first entry in matrix B will be:
B =
66 3

Antworten (1)

Azzi Abdelmalek
Azzi Abdelmalek am 19 Aug. 2014
A =[ 66 2
66 3
66 4
67 6
67 3
68 5
69 6
69 3]
[ii,jj,kk]=unique(A(:,1))
out=[ii accumarray(kk,A(:,2),[],@mean)]

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by