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!