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?
采纳的回答
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 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!