Finding Minimum Value, Setting it only if it has not already been selected, Else Finding Next Minimum...
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
beamer benz
el 8 de Mzo. de 2015
Comentada: Star Strider
el 9 de Mzo. de 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 comentarios
Respuesta aceptada
Star Strider
el 8 de Mzo. de 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 comentarios
Más respuestas (1)
Ver también
Categorías
Más información sobre Get Started with MATLAB en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!