# Are there any functions to randomly pick the minimum element from a vector if the vector has multiple minimum elements?

3 views (last 30 days)

Show older comments

Function min() picks the first minimum element from a vector if the vector has multiple minimum elements.

I want to randomly pick the minimum element from all the minimum elements and get its value and index.

Could you tell me if there is a function to do that?

##### 0 Comments

### Accepted Answer

Walter Roberson
on 15 Jul 2020

##### 5 Comments

Walter Roberson
on 15 Jul 2020

NR = 3;

[sortedvals, sortidx] = sort(YourVector);

LastSameIdx = 1;

MinVals = zeros(1,NR);

RandomIdx = zeros(1,NR);

for K = 1 : NR

LastSameIdx = 1;

while LastSameIdx < length(YourVector) && sortedvals(1) == sortedvals(LastSameIdx+1)

LastSameIdx = LastSameIdx + 1;

end

thisidx = randi(LastSameIdx);

MinVals(K) = sortedvals(thisidx);

RandomIdx(K) = sortidx(thisidx);

sortedvals(thisidx) = [];

sortidx(thisidx) = [];

end

Outputs are MinVals and RandomIdx.

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!