フィルターのクリア

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 件のコメント
madhan ravi
madhan ravi 2019 年 4 月 3 日
Which version of matlab?
Deepika Vatsa
Deepika Vatsa 2019 年 4 月 3 日
It's Matlab R2015a

サインインしてコメントする。

採用された回答

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 Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by