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 Commenti

Liz
Liz il 21 Lug 2011
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.

Accedi per commentare.

 Risposta accettata

the cyclist
the cyclist il 21 Lug 2011

0 voti

You want the ismember() function.

3 Commenti

Liz
Liz il 21 Lug 2011
thanks, that's useful.. but that doesn't give you the location of duplicates as well, does it?
Liz
Liz il 21 Lug 2011
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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by