Effacer les filtres
Effacer les filtres

finding all instances in a vector

19 vues (au cours des 30 derniers jours)
Liz
Liz le 21 Juil 2011
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 commentaires
Liz
Liz le 21 Juil 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]
the cyclist
the cyclist le 21 Juil 2011
No problem that this is a basic question! You asked it clearly and specifically, with an example of what you wanted. Good question.

Connectez-vous pour commenter.

Réponse acceptée

the cyclist
the cyclist le 21 Juil 2011
You want the ismember() function.
  3 commentaires
Liz
Liz le 21 Juil 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!
the cyclist
the cyclist le 21 Juil 2011
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by