MATLAB Answers

0

how to find elements in an array

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

로그인 to comment.

태그

답변 수: 2

Azzi Abdelmalek 님의 답변 18 Feb 2014
Azzi Abdelmalek 님이 편집함. 18 Feb 2014
 채택된 답변

x = 1:100;
y = 201:300;
a = [250; 215; 283];
out=x(ismember(y,a))

  댓글 수: 1

Brilliant, thank you!

로그인 to comment.


Thomas 님의 답변 18 Feb 2014
Thomas 님이 편집함. 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

thanks Thomas!

로그인 to comment.



Translated by