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:
mantain rows on matrix(ismember(matrix,vector))

Subject: mantain rows on matrix(ismember(matrix,vector))

From: Fogato Abbestia

Date: 6 Jul, 2013 07:56:08

Message: 1 of 3

Hi !

I need to recover elements of a matrix that are in a vector.

for now i do

a =

    'a' 'a' 'b'
    'b' 'c' 'd'
    'f' 'a' 'b'

>> b={'a' 'b' 'h'}

b =

    'a' 'b' 'h'

>> ismember(a,b)

ans =

     1 1 1
     1 0 0
     0 1 1

>> a(ismember(a,b))

ans =

    'a'
    'b'
    'a'
    'a'
    'b'
    'b'

but what i want is to keep the rows structure.

The output i want is

'a' 'a' 'b'
'b' [] []
'a' 'b' []

where [] are empty elements

it will be ok even save each single row in a cell array like this

arrcell={{'a' 'a' 'b'} {'b'} {'a' 'b'}}

thx

Subject: mantain rows on matrix(ismember(matrix,vector))

From: Sadik

Date: 6 Jul, 2013 17:19:10

Message: 2 of 3

Hi Fagota,

How about the following?

c = a;
M = ~ismember(a,b);
c(M) = cell(sum(M(:)),1)

Best.

Subject: mantain rows on matrix(ismember(matrix,vector))

From: Sadik

Date: 7 Jul, 2013 00:31:10

Message: 3 of 3

Another solution:

c = a;
c(~ismember(a,b)) = {[]}

Tags for 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