user1759208
user1759208

Reputation: 41

Finding a min of vector that index is a member of another vector

I have a vector matrix of a range of values, e.g:

x = [9 8 6 7 4 5 1 2];

I then have another vector that contains the indices of x values that match a particular criteria, e.g:

y = [7 8]; % (that is, elements 7 and 8 of x meet criteria)

I now need to find the minimum x, which has its index a member of y.

I have tried the following:

find(x == min(x) & ismember(find(x == min(x)), f) == 1)

But it doesn't work, it seems to just return a binary answer and even then, still doesn't work. The idea was to find an index that is minimum and is a member using find (where I take the index of the min x for the ismember).

Any help is appreciated!

Upvotes: 1

Views: 60

Answers (1)

Stewie Griffin
Stewie Griffin

Reputation: 14939

How about:

value = min(x(y));
value =  1

x(y) gives you the elements of x that are indexed by y. min is used to find only the smallest one.

Upvotes: 1

Related Questions