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에서 Performance and Memory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!