Find repeated row from col1, average repeated from col2, erase repeated row

1 回表示 (過去 30 日間)
Dave
Dave 2014 年 10 月 28 日
回答済み: the cyclist 2014 年 10 月 28 日
Hi, I have matrix A (7x2) where the first column has some repeated rows
A=[54.5 300; 56 250; 57 100; 56 600; 56.5 700; 57 800; 58 900]
In A, the number 56 repeats two times, the number 57 also repeats two times. [a number can be repeated a max of two times]
Whenever the number from the 1st column repeats, I need to take the average from the second column [so for 56 would be (250+600)*0.5=425] and re-express the matrix with that average in col2, and erase the repeated rows.
So the resulting matrix B would be 5x2 like this:
B= [54.5 300; 56 425; 56.5 700; 57 450; 58 900]
Any ideas please? I tried with ismember but cannot work with 0 indices. Thanks Dave

採用された回答

the cyclist
the cyclist 2014 年 10 月 28 日
[B1,~,j] = unique(A(:,1));
B2 = accumarray(j,A(:,2),[],@mean);
B = [B1,B2]

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by