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:
Creating a matrix of 1's and 0's by comparing values in two other matrixes

Subject: Creating a matrix of 1's and 0's by comparing values in two other matrixes

From: Ulrik Nash

Date: 11 May, 2010 14:38:05

Message: 1 of 4

Hi Everyone,

Suppose I have a matrix A of 1's and 0's:

A =

1 0 1
0 0 1
1 1 0

and another matrix B of 1's and 0's

B =

1 1 0
1 0 0
0 1 0

now I wish to create a third matrix C of same dimension and A and B, which contains 1's where A = B = 1, and 0's otherwise:

C =

1 0 0
0 0 0
0 1 0

How can this be done?

/Ulrik.

Subject: Creating a matrix of 1's and 0's by comparing values in two other matrixes

From: Jos (10584)

Date: 11 May, 2010 14:53:09

Message: 2 of 4

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <hsbq4d$f2s$1@fred.mathworks.com>...
> Hi Everyone,
>
> Suppose I have a matrix A of 1's and 0's:
>
> A =
>
> 1 0 1
> 0 0 1
> 1 1 0
>
> and another matrix B of 1's and 0's
>
> B =
>
> 1 1 0
> 1 0 0
> 0 1 0
>
> now I wish to create a third matrix C of same dimension and A and B, which contains 1's where A = B = 1, and 0's otherwise:
>
> C =
>
> 1 0 0
> 0 0 0
> 0 1 0
>
> How can this be done?
>
> /Ulrik.

One of the many, many ways:

C = A .* B

hth
Jos

Subject: Creating a matrix of 1's and 0's by comparing values in two other matrixes

From: Ulrik Nash

Date: 11 May, 2010 15:10:08

Message: 3 of 4

"Jos (10584) " <#10584@fileexchange.com> wrote in message <hsbr0l$cip$1@fred.mathworks.com>...
> "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <hsbq4d$f2s$1@fred.mathworks.com>...
> > Hi Everyone,
> >
> > Suppose I have a matrix A of 1's and 0's:
> >
> > A =
> >
> > 1 0 1
> > 0 0 1
> > 1 1 0
> >
> > and another matrix B of 1's and 0's
> >
> > B =
> >
> > 1 1 0
> > 1 0 0
> > 0 1 0
> >
> > now I wish to create a third matrix C of same dimension and A and B, which contains 1's where A = B = 1, and 0's otherwise:
> >
> > C =
> >
> > 1 0 0
> > 0 0 0
> > 0 1 0
> >
> > How can this be done?
> >
> > /Ulrik.
>
> One of the many, many ways:
>
> C = A .* B
>
> hth
> Jos

Thanks Jos!

Subject: Creating a matrix of 1's and 0's by comparing values in two other matrixes

From: Tom

Date: 11 May, 2010 15:44:06

Message: 4 of 4

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <hsbq4d$f2s$1@fred.mathworks.com>...
> Hi Everyone,
>
> Suppose I have a matrix A of 1's and 0's:
>
> A =
>
> 1 0 1
> 0 0 1
> 1 1 0
>
> and another matrix B of 1's and 0's
>
> B =
>
> 1 1 0
> 1 0 0
> 0 1 0
>
> now I wish to create a third matrix C of same dimension and A and B, which contains 1's where A = B = 1, and 0's otherwise:
>
> C =
>
> 1 0 0
> 0 0 0
> 0 1 0
>
> How can this be done?
>
> /Ulrik.

More general way (that could work with other thing then 1 and zero ) :
C = (A == 1 & B ==1)

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