フィルターのクリア

Number of rows based on frequency of one integer and position of another integer

2 ビュー (過去 30 日間)
Neuro
Neuro 2022 年 1 月 17 日
コメント済み: Neuro 2022 年 1 月 18 日
Hello. I was wondering if someone could help me with this:
Let's say you have a 2000 x 2 matrix, where all the elements are integers. How can we count the number of rows that contain the number 10 in its first column and any number with the highest 5 integers in the second column?
In other words, the following but where X is the fifth highest number in the second column:
nnz(matrix(:,1) == 10 & matrix(:,2) > X);
EDIT -> Very importantly, the list with the highest 5 integers must be determined for each integer in the first column. So, if one wanted to do this for the number 10, it must be determining the highest 5 integers in the second column WHEN the integer in the first column is 10.
Thanks!

採用された回答

Voss
Voss 2022 年 1 月 17 日
Get the set of unique numbers in the 2nd column:
col2_vals = unique(matrix(:,2));
The 5th highest is 4th from the end. So, to match any number within the highest 5:
nnz(matrix(:,1) == 10 & matrix(:,2) >= col2_vals(end-4));
  4 件のコメント
Voss
Voss 2022 年 1 月 18 日
idx = matrix(:,1) == 10;
col2_vals = unique(matrix(idx,2)); % now: unique values of column 2 where column 1 is 10
nnz(matrix(idx,2) >= col2_vals(end-4));
Neuro
Neuro 2022 年 1 月 18 日
Thanks @Benjamin This solves my problem! :)

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

その他の回答 (1 件)

David Hill
David Hill 2022 年 1 月 17 日
r=unique(matrix(:,2));
r=r(end-4:end);
n=nnz(matrix(:,1)==10&ismember(matrix(:,2),r));
  3 件のコメント
David Hill
David Hill 2022 年 1 月 18 日
r=unique(matrix(matrix(:,1)==10,2));
r=r(end-4:end);
n=nnz(matrix(:,1)==10&ismember(matrix(:,2),r));
Neuro
Neuro 2022 年 1 月 18 日
Thanks @David Hill, but it gives me an error message:
Error using unique (line 117)
Invalid input. Valid flags are 'rows', 'first', 'last', 'stable', 'sorted', 'legacy'.

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by