zdm
zdm

Reputation: 531

Find the largest index of the minimum in Matlab

I have an array of positive numbers and there are some duplicates. I want to find the largest index of the minimum value.

For example, if a=[2, 3, 1, 1, 4, 1, 3, 2, 1, 5, 5] then [i, v] = min(a) returns i=3, however I want i=9.

Upvotes: 1

Views: 254

Answers (3)

OmG
OmG

Reputation: 18858

You can use imreginalmin as well with time complexity O(n):

largestMinIndex = find(imregionalmin(A),1,'last');

Upvotes: 2

Paolo
Paolo

Reputation: 26275

Here's a different idea, which only requires one function, sort:

[~,y] =  sort(a,'descend');
i = y(end)

ans =

     9

Upvotes: 3

jualsevi
jualsevi

Reputation: 289

Using find and min.

A = [2, 3, 1, 1, 4, 1, 3, 2, 1, 5, 5];
minA = min(A);
maxIndex = max(find(A==minA));

min get the minimun value, and find return de index of values that meet the condition A==minA. max return de maximun index.

Upvotes: 3

Related Questions