finding all instances in a vector

11 ビュー (過去 30 日間)
Liz
Liz 2011 年 7 月 21 日
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 件のコメント
Liz
Liz 2011 年 7 月 21 日
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 2011 年 7 月 21 日
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 日
You want the ismember() function.
  3 件のコメント
Liz
Liz 2011 年 7 月 21 日
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 2011 年 7 月 21 日
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 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by