how to select from identical rows based on their associated value in one column?
1 回表示 (過去 30 日間)
古いコメントを表示
I have a matrix of 2 columns and lots of rows. First column has numeric values that occurred twice and second column has probabilities associated with it.
for e.g., a = [1 , 0.6
2 , 0.9
1, 0.4
3 , 0.5
4 , 0.2
2 , 0.1
4 , 0.8
3 , 0.5]
I want choose rows which have elements with higher probability i.e.,
ans = [1 , 0.6
2 , 0.9
4 , 0.8]
I want ignore rows with same probability. How to do this in fastest manner possible?
2 件のコメント
採用された回答
Star Strider
2019 年 4 月 3 日
Another approach using accumarray:
a = [1 , 0.6
2 , 0.9
1, 0.4
3 , 0.5
4 , 0.2
2 , 0.1
4 , 0.8
3 , 0.5];
[Ua,~,ix] = unique(a(:,1)); % Unique Values & Indices
L = accumarray(ix, a(:,2), [], @(x)max(sum(x)~=2*x)); % Eliminate Duplicates (Returns Logical Vector)
R = accumarray(ix, a(:,2), [], @max); % Maximum Probabilities
Out = [Ua(L), R(L)] % Report Only Maxima For Non-Duplicated Probabilities
producing:
Out =
1 0.6
2 0.9
4 0.8
4 件のコメント
Star Strider
2019 年 4 月 4 日
As always, my pleasure.
I had to think about that.
This works:
a = [1 , 0 , 0.6
1 , 1 , 0.4
2 , 0 , 0.1
2 , 1 , 0.9];
[Ua,~,ix] = unique(a(:,1)); % Unique Values
L = accumarray(ix, a(:,3), [], @(x)max(sum(x)~=2*x)); % Eliminate Duplicates (Returns Logical Vector)
R = accumarray(ix, a(:,3), [], @max); % Maximum Probabilities
S = accumarray(ix, a(:,3), [], @(x) a(find(x==max(x)),2)); % Get ‘sign’
Out = [Ua(L) S(L), R(L)] % Report Only Maxima & Sign For Non-Duplicated Probabilities
producing:
Out =
1 0 0.6
2 1 0.9
その他の回答 (1 件)
Bob Thompson
2019 年 4 月 3 日
編集済み: Bob Thompson
2019 年 4 月 3 日
There might be some fancy function I don't know, but it's certainly possible to do this with a loop. I cannot comment on the speed of this method.
a = [1 , 0.6
2 , 0.9
1, 0.4
3 , 0.5
4 , 0.2
2 , 0.1
4 , 0.8
3 , 0.5];
uni = unique(a(:,1));
b = [];
for i = 1:length(uni)
b(i,:) = a(a(:,2) == max(a(a(:,1)==uni(i),:)),:);
end
EDIT* Changed from uniques in second column to uniques in first column. I misread the question.
4 件のコメント
madhan ravi
2019 年 4 月 3 日
編集済み: madhan ravi
2019 年 4 月 3 日
Bob,I am not able to run your code but according to what I understand from the question is that if the probabilities are identical OP wants to omit those rows and find the max of probabilities of each group (which belong to column 1). Haven’t tested with the timings though.
参考
カテゴリ
Help Center および File Exchange で Performance and Memory についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!