MATLAB: Could anyone help me how to display the top three highest values in each column and display the other values to be zero.

matrix manipulation

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.

Best Answer

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))]