how to find elements in an array

274 views (last 30 days)
Chad Greene on 18 Feb 2014
Commented: Chad Greene on 18 Feb 2014
Given this:
x = 1:100;
y = 201:300;
a = 250;
I know that
x(y==a)
will return 50, the value of x corresponding to the y index where y = a. Now if a has multiple elements, I'd like to find all the values of x corresponding to all the values of y where y equals any of the values in a. For example, if
a = [250; 215; 283];
I'd like to efficiently get the array [50; 15; 83]. How can I do this?
0 CommentsShowHide -1 older comments

Sign in to comment.

Accepted Answer

Azzi Abdelmalek on 18 Feb 2014
Edited: Azzi Abdelmalek on 18 Feb 2014
x = 1:100;
y = 201:300;
a = [250; 215; 283];
out=x(ismember(y,a))
1 CommentShowHide None
Chad Greene on 18 Feb 2014
Brilliant, thank you!

Sign in to comment.

More Answers (1)

Thomas on 18 Feb 2014
Edited: Thomas on 18 Feb 2014
x = 1:100;
y = 201:300;
a=[ 215 250 283];
c=ismember(y,a)
x(c)
should give you
15 50 83
1 CommentShowHide None
Chad Greene on 18 Feb 2014
thanks Thomas!

Sign in to comment.

Categories

Find more on Naming Conventions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by