Filter löschen
Filter löschen

How to reduce this matrix?

1 Ansicht (letzte 30 Tage)
Anderson
Anderson am 21 Apr. 2016
Beantwortet: Azzi Abdelmalek am 21 Apr. 2016
How to reduce this matrix?
Suppose that M=[1 0.1 1.1; 1 0.1 1.1; 3 0.1 1.2; 2 0.2 3; 1 2 4; 2 2 4; 2 3 5];
based on the second column, for repeated values of the second column, sum the elements of column 1 when the elements of column 3 are equals. If the elements of column 3 are different, do a weighted average using column 1.
For the matrix M above, the output should be:
M_out = [5 0.1 1.16; 2 0.2 3; 3 2 4; 2 3 5];

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 21 Apr. 2016
M=[1 0.1 1.1; 1 0.1 1.1; 3 0.1 1.2; 2 0.2 3; 1 2 4; 2 2 4; 2 3 5]
[ii,jj,kk]=unique(M(:,2))
a=accumarray(kk,M(:,1))
b=accumarray(kk,1:numel(kk),[],@(x) sum(M(x,1).*M(x,3))/sum(M(x,1)));
M_out =[a ii b]

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by