フィルターのクリア

Separate numbers present within a cell on multiple rows

2 ビュー (過去 30 日間)
Alberto Acri
Alberto Acri 2023 年 8 月 2 日
回答済み: dpb 2023 年 8 月 2 日
Hi. I retrieved the first N numbers (largest to smallest) from the second column of the 'CountArray' array.
Now I wanted to determine the corresponding value on the first column of the 'CountArray' array by creating a column vector with these values.
I tried this way but, for example on row 22 and row 23 of cell 'matrix', there are two (equal) values because in 'values_rec' there is the same number (24).
CountArray = importdata("CountArray.mat");
N = 30;
values_rec = maxk(CountArray(:,2),N);
matrix = {};
for K = 1:height(values_rec)
[row,col] = find(CountArray(:,2) == values_rec(K));
value = CountArray(row);
matrix = [matrix;{value}];
end
matrix = cell2mat(matrix);
So I have to transform 'matrix' in this way:

採用された回答

Voss
Voss 2023 年 8 月 2 日
編集済み: Voss 2023 年 8 月 2 日
Take advantage of the second output of maxk, which is a vector of indices of the maxk values.
CountArray = importdata("CountArray.mat");
N = 30;
[values_rec,idx] = maxk(CountArray(:,2),N);
matrix = CountArray(idx,1);
disp(matrix)
71 70 69 72 68 73 74 67 75 76 77 66 78 79 80 81 65 82 83 64 84 85 90 94 86 93 89 95 134 87

その他の回答 (1 件)

dpb
dpb 2023 年 8 月 2 日
whos -file CountArray.mat
Name Size Bytes Class Attributes CountArray 86x2 1376 double
load CountArray.mat
N = 30;
[values_rec,ix] = maxk(CountArray(:,2),N);
matrix=CountArray(ix,1);
matrix(19:30)
ans = 12×1
83 64 84 85 90 94 86 93 89 95
See maxk doc; return the optional output of the location as well and you've already got the answer.

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by