finding all instances in a vector

Hi all,
is there an easy way to find all instances of, say, numbers in vector x in a target vector, y?
For example, if I have a vector x which is: [ 1 4 10 ] how do i find all 1's 4's and 10's in y: [ 1 2 3 4 5 6 7 8 9 10 ]
sorry if this is a basic question.. i could write a for-loop of course, but i imagine there must be a more elegant way to do this. Thanks in advance!

2 个评论

sorry, i have to add one other thing.. it actually has to find all instances, even duplicates.. so it would be more like
x = [1 4 10] and y = [1 2 3 4 4 4 5 4 6 4 7 8 9 10]
No problem that this is a basic question! You asked it clearly and specifically, with an example of what you wanted. Good question.

请先登录,再进行评论。

 采纳的回答

the cyclist
the cyclist 2011-7-21

0 个投票

You want the ismember() function.

3 个评论

thanks, that's useful.. but that doesn't give you the location of duplicates as well, does it?
oh! i get it now, you were right..
if i have
x = [1 4 10] and y = [1 2 3 4 4 4 5 4 6 4 7 8 9 10]
i can say: out = ismember(y,x) and i get:
out =
Columns 1 through 14
1 0 0 1 0 0 0 0 0 1 1 0 0 1
Columns 15 through 20
0 0 0 0 0 1
thank you!
Note that if you call ismember() with two outputs, then the first output indicates that the element was found, and the second output is an index to WHICH element of "x" it was.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by