1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| function [MaxValueVector,MaxIndexVector] = SortNValues(Matrix,n)
MaxValueVector = zeros(1,n); MaxIndexVector = zeros(2,n);
if(size(Matrix,1)==0||size(Matrix,2)==0) return; end
for i = 1:n MaxValueVector(1,i) = max(Matrix,[],'all'); [MaxIndexRow,MaxIndexCol] = find(Matrix==max(Matrix,[],'all')); MaxIndexRow1 = 0; MaxIndexCol1 = 0; if(size(MaxIndexRow,1)~=1||size(MaxIndexCol,2)~=1) MaxIndexRow1 = MaxIndexRow(1); MaxIndexCol1 = MaxIndexCol(1); else MaxIndexRow1 = MaxIndexRow; MaxIndexCol1 = MaxIndexCol; end
MaxIndexVector(:,i) = [MaxIndexRow1;MaxIndexCol1]; Matrix(MaxIndexRow1,MaxIndexCol1) = -Matrix(MaxIndexRow1,MaxIndexCol1); end end
|