Find set of values that are unique to the values in another column

1 回表示 (過去 30 日間)
Elian Ricardo Gonzalez Beltran
Elian Ricardo Gonzalez Beltran 2019 年 11 月 22 日
回答済み: Stephen23 2019 年 11 月 22 日
I have a matrix like the following.
2 3
2 6
2 8
4 5
5 9
5 39
5 6
I need to do operations on numbers in the second column based on the values in the first column. It should be for the unique vaue "2", mean of (3,6,8) etc. Any help is appreciated. Thanks

採用された回答

Erivelton Gualter
Erivelton Gualter 2019 年 11 月 22 日
% Mqtrix
A =[2 3;
2 6
2 8
4 5
5 9
5 39
5 6]
% Get unique values on first collumn
un = unique(A(:,1)); % un = [2 4 5]
% Output
A(A(:,1)==un(1),2)

その他の回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2019 年 11 月 22 日
[G,d] = findgroups(A(:,1));
out = [d,splitapply(@mean,A(:,2),G)];

Stephen23
Stephen23 2019 年 11 月 22 日
>> M = [2,3;2,6;2,8;4,5;5,9;5,39;5,6]
M =
2 3
2 6
2 8
4 5
5 9
5 39
5 6
>> [U,~,X] = unique(M(:,1));
>> V = accumarray(X,M(:,2),[],@mean);
>> [U,V]
ans =
2.0000 5.6667
4.0000 5.0000
5.0000 18.0000

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by