필터 지우기
필터 지우기

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
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
Deepika Vatsa
Deepika Vatsa 2019년 4월 4일
Hey! I have got it. Using intersect I got the indexes for rows with correct sign as well. Your solution is fastest. Many thanks.
Star Strider
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
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
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.
Deepika Vatsa
Deepika Vatsa 2019년 4월 4일
Thank you Bob and Madhan for the solutions! I have got my solution by first eliminating same probability rows using 'unique' and then using 'varfun' as suggested by Madhan. Bob, my matrix has lots of rows(in millions), I don't think using a for loop would help me with computation time. So, I think using varfun is quicker. Thanks again.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by