MATLAB: Finding Minimum Value, Setting it only if it has not already been selected, Else Finding Next Minimum…

minimum

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.

Best Answer

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)
Related Question