several minimum values on a vector

52 visualizzazioni (ultimi 30 giorni)
Sherwin
Sherwin il 30 Apr 2022
Risposto: Walter Roberson il 30 Apr 2022
I want to use the min function to find the index of the element with minimum value on vector Z:
[X,Y] = min(Z,[],1);
how can I detect when there are several elements with the same value which are all minimums and assign a another value to those indices?

Risposte (2)

Riccardo Scorretti
Riccardo Scorretti il 30 Apr 2022
I'm not sure to understand completely the question, hower you can use find to get all the values which are strictly equal to the minimum value. The index returned by min is the first one, so to get the index of other items it is enough to get rid of the first one.
In the following example the minimum is 1, and it is found in positions 4, 6 and 7.
z = [ 2, 3, 5, 1, 2, 1, 1, 9];
[val, ind] = min(z)
val = 1
ind = 4
other_ind = find(z == val)
other_ind = 1×3
4 6 7
other_ind = other_ind(2:end)
other_ind = 1×2
6 7
  1 Commento
Riccardo Scorretti
Riccardo Scorretti il 30 Apr 2022
Of course, you can use other_ind to affect a different value (which one, by the way?) to those elements.

Accedi per commentare.


Walter Roberson
Walter Roberson il 30 Apr 2022
Provided you are dealing with a vector
X = min(Z);
Y = find(Z == X);
Generalizing this to the case of arrays (2 or more dimensions) gets a bit trickier to vectorize, since the indices returned by min() are relative to the dimension rather than being absolute indices -- that and the fact that in one column there might be only one value that is the min for the column, but another column might have more than one copy of the min for that column, so the number of values to return per column could vary. Makes vectorization tricky.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by