Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
find all numbers in a vector

Subject: find all numbers in a vector

From: Petra

Date: 4 Dec, 2012 00:07:10

Message: 1 of 5

Hi all,
I cant find a solution without a loop for this problem:
I got 2 vectors
 a=[2 3 14 15 16 17 18 33 34 35]
b=[17 18 35 60 61 62]
and I try to find all numbers and position in a which are in b.
The answer should be: 6 7 8 10
Thanks for help

Subject: find all numbers in a vector

From: Nasser M. Abbasi

Date: 4 Dec, 2012 01:18:06

Message: 2 of 5

On 12/3/2012 6:07 PM, Petra wrote:
> Hi all,
> I cant find a solution without a loop for this problem:
> I got 2 vectors
> a=[2 3 14 15 16 17 18 33 34 35]
> b=[17 18 35 60 61 62]
> and I try to find all numbers and position in a which are in b.
> The answer should be: 6 7 8 10
> Thanks for help
>

You answer is wrong. Here is the correct answer

EDU>> find(ismember(a,b)==1)

ans =

      6 7 10

Subject: find all numbers in a vector

From: Petra

Date: 4 Dec, 2012 02:17:09

Message: 3 of 5

you are right

thank you very very much

Subject: find all numbers in a vector

From: Roger Stafford

Date: 4 Dec, 2012 03:00:12

Message: 4 of 5

"Petra" wrote in message <k9jerd$h6o$1@newscl01ah.mathworks.com>...
> a=[2 3 14 15 16 17 18 33 34 35]
> b=[17 18 35 60 61 62]
> and I try to find all numbers and position in a which are in b.
> The answer should be: 6 7 8 10

"Petra" wrote in message <k9jmf4$boc$1@newscl01ah.mathworks.com>...
> you are right
- - - - - - - - - -
  You shouldn't give up so quickly, Petra. The answer you gave was correct - for the positions in 'a'.

  Use both returned quantities in the function 'ismember' to determine both the numbers in 'a' which are also in 'b' and their positions. Bear in mind of course that in case there is more than one like number in 'a' which is duplicated in 'b', you will get the position of the one with highest index.

Roger Stafford

Subject: find all numbers in a vector

From: Roger Stafford

Date: 4 Dec, 2012 03:24:08

Message: 5 of 5

"Roger Stafford" wrote in message <k9jovs$jnl$1@newscl01ah.mathworks.com>...
> You shouldn't give up so quickly, Petra. The answer you gave was correct - for the positions in 'a'.
- - - - - - -
  Oops! I was hasty in reading your answer. As Nassar indicates, the 8 is incorrect. However, it is convenient to use 'ismember' if you want to find both the like numbers and their position.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us