how to find values in two different vectors ?

조회 수: 2 (최근 30일)
Niki
Niki 2014년 2월 7일
댓글: Azzi Abdelmalek 2014년 2월 7일
I have two vectors e.g.
a=[2 7 9 10 17 22 24 28 29 32]
b=[74 79 82 85 88 91 92 95 96 97 98 99 102 108 121]
I want to find several values in two vector. for example 9, 24 , 85 and 102.
I first make
m=[9;24;85;102]
for i=1:size(m,1)
mt=find (a==m (i))
if a==0
else mtt=find (b==m(i))
end
end
However, when I run such script I cannot see which value is in which vector because each time mt or mtt will be empty in next epoch. I want to have the find result in mt for each run and if it is empty , I want to mention zero
for example I want to have mt and mtt as follows
mt=[3 7]
mtt=[4 13]
Thanks
  댓글 수: 1
Azzi Abdelmalek
Azzi Abdelmalek 2014년 2월 7일
You could just ask for a. You can do the same thing for b

댓글을 달려면 로그인하십시오.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 2월 7일
a=[2 7 9 10 17 22 24 28 29 32];
b=[74 79 82 85 88 91 92 95 96 97 98 99 102 108 121]
m=[9;24;85;102]
mt=find(ismember(a,m))
mtt=find(ismember(b,m))
  댓글 수: 2
Niki
Niki 2014년 2월 7일
Azzi, Thank you very much for your help. This works just fine but I want to know where my problem is when I use loop as above mentioned. can you please help me with it?
Azzi Abdelmalek
Azzi Abdelmalek 2014년 2월 7일
I can't explain what your code is doing, I can propose one correct way to do it with a for loop
a=[2 7 9 10 17 22 24 28 29 32];
b=[74 79 82 85 88 91 92 95 96 97 98 99 102 108 121];
m=[9;24;85;102];
mt=[];
mtt=[];
for i=1:size(m,1)
mt=[mt find(a==m(i))];
mtt=[mtt find(b==m(i))];
end
mt
mtt

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Search Path에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by