Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
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?

Réponse acceptée

Star Strider
Star Strider le 20 Avr 2017
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.
  5 commentaires
Utsav Vishal
Utsav Vishal le 20 Avr 2017
yaa...Thank You, sir. I will check with my data. Best Regards.

Connectez-vous pour commenter.

Plus de réponses (2)

KSSV
KSSV le 20 Avr 2017
doc min
A = rand(5,2) ;
[val,idx] = min(A,[],1) ;
B = [val ; A]
  3 commentaires
KSSV
KSSV le 20 Avr 2017
A = rand(5,2) ;
[nx,ny] = size(A) ;
[val,idx] = min(A,[],1) ;
p = sub2ind(size(A),idx,1:ny) ;
B = A ;
B(idx) = [] ;
B =reshape(B,nx-1,ny) ;
B = [val; B] ;
Utsav Vishal
Utsav Vishal le 20 Avr 2017
Modifié(e) : Utsav Vishal le 20 Avr 2017
The code is just taking the minimum value to the 1st position, and deleting it from its original position. also, the 1st element of second colomn becomes the last element of 1st column. I have to deal with each column separately. eg[5 4 3 1 2, 8 6 4 5 7] to [1 2 5 4 3, 4 5 7 8 6]. Thank you.

Connectez-vous pour commenter.


Roger Stafford
Roger Stafford le 20 Avr 2017
[~,I] = min(A,[],1);
for k = 1:size(A,2);
A(:,k) = circshift(A(:,k),1-I(k),1);
end
  5 commentaires
Roger Stafford
Roger Stafford le 20 Avr 2017
Given the error you received, try this instead:
[~,I] = min(A,[],1);
for k = 1:size(A,2);
A(:,k) = circshift(A(:,k),1-I(k));
end
Utsav Vishal
Utsav Vishal le 20 Avr 2017
Thank You, sir. I will check with my data. Best Regards.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by