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 matrix of 1's and 0's based on a comparison of values in two other matrixes

Subject: Creating matrix of 1's and 0's based on a comparison of values in two other matrixes

From: Ulrik Nash

Date: 11 May, 2010 13:13:04

Message: 1 of 3

Hi Everyone,

I have two matrixes:

A =

     1 2
     3 4
     5 1

and

B =

     1 3

Now I wish to create matrix C, which has the same dimensions as A. C should look like this:

C =

0 0
1 1
1 0

C is generated by 'asking' for every number in a column of A, if that number is smaller than or equal to the number in the corresponding column of B. If this is the case, then a 0 is entered in C or else 1 is entered.

I am sure there is a simple way of doing this in Matlab, but I haven't found it.

I appreciate the help.

/Ulrik.

Subject: Creating matrix of 1's and 0's based on a comparison of values in

From: Ashish Uthama

Date: 11 May, 2010 13:17:24

Message: 2 of 3

On Tue, 11 May 2010 09:13:04 -0400, Ulrik Nash <uwn@sam.sdu.dk> wrote:

> Hi Everyone,
>
> I have two matrixes:
>
> A =
>
> 1 2
> 3 4
> 5 1
>
> and B =
>
> 1 3
>
> Now I wish to create matrix C, which has the same dimensions as A. C
> should look like this:
>
> C = 0 0
> 1 1
> 1 0
>
> C is generated by 'asking' for every number in a column of A, if that
> number is smaller than or equal to the number in the corresponding
> column of B. If this is the case, then a 0 is entered in C or else 1 is
> entered.
>
> I am sure there is a simple way of doing this in Matlab, but I haven't
> found it.
>
> I appreciate the help.
>
> /Ulrik.


>> a=rand(3,2)

a =

     0.2277 0.9234
     0.4357 0.4302
     0.3111 0.1848

>> b=[.1 .4]

b =

     0.1000 0.4000

>> bsxfun(@(ai,bj)ai>bj,a,b)

ans =

      1 1
      1 1
      1 0

Subject: Creating matrix of 1's and 0's based on a comparison of values in two other matrixes

From: Jos (10584)

Date: 11 May, 2010 13:19:04

Message: 3 of 3

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <hsbl50$ivi$1@fred.mathworks.com>...
> Hi Everyone,
>
> I have two matrixes:
>
> A =
>
> 1 2
> 3 4
> 5 1
>
> and
>
> B =
>
> 1 3
>
> Now I wish to create matrix C, which has the same dimensions as A. C should look like this:
>
> C =
>
> 0 0
> 1 1
> 1 0
>
> C is generated by 'asking' for every number in a column of A, if that number is smaller than or equal to the number in the corresponding column of B. If this is the case, then a 0 is entered in C or else 1 is entered.
>
> I am sure there is a simple way of doing this in Matlab, but I haven't found it.
>
> I appreciate the help.
>
> /Ulrik.

help bsxfun

A = [1 2 ; 3 4 ; 51 1]
B = [1 3]
C = bsxfun(@lt, B, A)

hth
Jos

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