Could anyone help me how to display the top three highest values in each column and display the other values to be zero.
2 ビュー (過去 30 日間)
古いコメントを表示
I am having a matrix
A= [2.3433 2.8950 2.9967 3.8753 3.9190 3.9104 3.9563 3.9525;
0.5167 0.4839 0.4885 0.1197 0.2249 0.2497 0.2874 0.2798;
0.4429 0.4348 0.4009 0.2255 0.1793 0.2188 0.1807 0.1816;
0.3900 0.1836 0.1741 0.1292 0.1241 0.1093 0.1058 0.1280;
0.0792 0.1701 0.1765 0.1033 0.0859 0.0758 0.0681 0.0769;
0.2301 0.1068 0.1036 0.0698 0.0868 0.0945 0.0912 0.0847]
I want to display the top three highest values in each column and the rest of the other values should be displayed as zero.
Could anyone please help me on this.
0 件のコメント
採用された回答
Guillaume
2019 年 6 月 2 日
Getting the top 3 values of each column is trivial with maxk
top3 = maxk(A, 3, 1)
I don't see the point in having 0s below these values but if that's you really want, you can simply concatenate a zero matrix below that
top3padded = [mask(A, 3, 1); zeros(size(A, 1)-3, size(A, 2))]
2 件のコメント
Guillaume
2019 年 6 月 2 日
編集済み: Guillaume
2019 年 6 月 2 日
You're probably on a version earlier than 2017b. Please fill the release field on the top right of the page.
On old versions, you'll have to use sort as shown by Geoff.
edit: Actually looking at your desired output, it's slightly more complicated:
[~, roworder] = sort(A, 1, 'descend');
A(sub2ind(size(A), roworder(4:end, :), repmat(1:size(A, 2), size(A, 1)-3, 1))) = 0
その他の回答 (1 件)
Geoff Hayes
2019 年 6 月 2 日
B = sort(A, 'descend');
To set all but the first three columns to zero, then do
B(4:end,:) = 0;
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!