Sorting the columns of a matrix by "largest element" in matlab -
in matlab, how can sort matrices columns in ascending order largest element in given column.
for example, given matrix a=[1 300 5; 100 1 2; 2 200 7], output a=[300 1 5; 1 100 2; 200 2 7].
i can using loop, i've been hammered idea should matrix operation in matlab.
find maxima per column in , sort them. need indices of sort (i
).
>> [sortedmaxs,i]=sort(max(a,[],1),'descend') sortedmaxs = 300 100 7 = 2 1 3
sort a
based on indices i
:
>> asort=a(:,i) asort = 300 1 5 1 100 2 200 2 7
so in short, need these 2 lines:
[~,i]=sort(max(a,[],1),'descend'); asort=a(:,i);
Comments
Post a Comment