필터 지우기
필터 지우기

finding all instances in a vector

조회 수: 19 (최근 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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by