Finding Minimum Value, Setting it only if it has not already been selected, Else Finding Next Minimum...
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
beamer benz
le 8 Mar 2015
Commenté : Star Strider
le 9 Mar 2015
I have a function that finds the minimum value of a row (indexed by i) and returns the row and column.
I am saving the column of the first minimum value to indx. However, I want to check before I save it, if the column number has previously been chosen, and if so, select the next minimum value.
My current code is below:
[row1,col1]=find(dst(i,:)==min(dst(i,:))); % Find the minimum value and save
the row, col
indx(i,1)=col1(1); %Select the first minimum value
So, I tried
if (unique(indx==col1(1)))~=1
% Check entire indx to see if it matches to the first minimum value chosen and if it has already been selected
indx(i,1)=col1(1); % If not, select it
else % Else find the next minimum value and select that
[row2,col2]=find(dst(i,:)==min(setdiff(dst(i,:),min(dst(i,:)))));
indx(i,1)=col2(1);
However, the (unique(indx==col1(1))) seems to fail and not working correctly.
0 commentaires
Réponse acceptée
Star Strider
le 8 Mar 2015
I find it difficult to follow what you’re doing. The unique function itself may provide you with all the information you need. You can use it with three outputs, and sorts the first output (the unique values in the argument vector) in ascending order. You can then use either of the other arguments to determine the index of the first (second output) or all (third output) of the unique values.
Experiment with this code snippet to see how it works:
x = randi(10, 1, 15)
[Ux, ia, ic] = unique(x)
0 commentaires
Plus de réponses (1)
Voir également
Catégories
En savoir plus sur Get Started with MATLAB 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!