MATLAB: How can i position the minimum value in the first cell for each column, without changing the sequence

arrayfirst cellminimumsequence

I have a 100 x 1000 array, with the minimum value at the different position for each column. How can I position the minimum value in the first cell for each column, without changing the sequence?

Best Answer

This should do what you want:
M = randi(9,6,4); % Create Matrix
for k1 = 1:size(M,2)
[~,idx] = min(M(:,k1)); % Index Of Minimum In Column ‘k1’
Mr(:,k1) = circshift(M(:,k1), 1-idx, 1); % Rotate To Put First Minimum In First Row
end
M =
1 6 9 5
6 4 3 3
9 5 7 4
3 9 3 2
1 4 9 4
4 7 3 4
Mr =
1 4 3 2
6 5 7 4
9 9 3 4
3 4 9 5
1 7 3 3
4 6 9 4
Here ‘M’ is the original matrix, ‘Mr’ is the ‘rotated’ matrix. The circshift function will do what you want.
Note that the min (and max) functions only return the index of the first value of the minimum in a vector, if there are duplicates.