Re: Comparing Corresponding Columns of Two Matrices

• To: mathgroup at smc.vnet.net
• Subject: [mg97586] Re: [mg97496] Comparing Corresponding Columns of Two Matrices
• From: DrMajorBob <btreat1 at austin.rr.com>
• Date: Mon, 16 Mar 2009 04:24:21 -0500 (EST)
• References: <200903141039.FAA12977@smc.vnet.net>

But somebody gave a solution using Clip that's probably much faster.

Bobby

On Sun, 15 Mar 2009 17:22:07 -0500, Gregory Lypny
<gregory.lypny at videotron.ca> wrote:

> Dr. Bob,
>
> This is cool, a cornucopia of mapping and @'s, a tutorial to keep me
> busy for some time.
>
> Thanks,
>
> 	Gregory
>
> On Sat, Mar 14, 2009, at 4:46 PM, DrMajorBob wrote:
>
>> If I understand the problem correctly, then... in order of increasing
>> speed or simplicity (I think):
>>
>> x = RandomInteger[{0, 3}, {10, 4}]
>> y = RandomInteger[{0, 3}, {10, 4}]
>>
>> {{3, 3, 0, 3}, {3, 1, 2, 3}, {1, 2, 1, 1}, {0, 1, 3, 1}, {1, 2, 1,
>>  2}, {0, 2, 3, 2}, {0, 1, 0, 3}, {0, 3, 1, 1}, {0, 0, 1, 1}, {0, 0,
>>  3, 2}}
>>
>> {{0, 0, 3, 3}, {2, 1, 1, 1}, {1, 3, 2, 2}, {3, 1, 1, 3}, {0, 0, 2,
>>  0}, {2, 1, 0, 2}, {2, 3, 3, 2}, {0, 0, 2, 1}, {3, 1, 2, 2}, {3, 3,
>>  1, 1}}
>>
>>
>> {{1, 1, 0, 0, 1, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 1, 1, 0, 1, 0, 0}, {0,
>>  1, 0, 1, 0, 1, 0, 0, 0, 1}, {0, 1, 0, 0, 1, 0, 1, 0, 0, 1}}
>>
>> or
>>
>> Map[Boole, Positive /@ (Transpose@x - Transpose@y), {2}]
>>
>> {{1, 1, 0, 0, 1, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 1, 1, 0, 1, 0, 0}, {0,
>>  1, 0, 1, 0, 1, 0, 0, 0, 1}, {0, 1, 0, 0, 1, 0, 1, 0, 0, 1}}
>>
>> or
>>
>> f[a_] = Boole@Positive@a;
>> SetAttributes[f, Listable]
>> f /@ (Transpose@x - Transpose@y)
>>
>> {{1, 1, 0, 0, 1, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 1, 1, 0, 1, 0, 0}, {0,
>>  1, 0, 1, 0, 1, 0, 0, 0, 1}, {0, 1, 0, 0, 1, 0, 1, 0, 0, 1}}
>>
>> or
>>
>> f[a_] = Boole@Positive@a;
>> SetAttributes[f, Listable]
>> f /@ Transpose[x - y]
>>
>> {{1, 1, 0, 0, 1, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 1, 1, 0, 1, 0, 0}, {0,
>>  1, 0, 1, 0, 1, 0, 0, 0, 1}, {0, 1, 0, 0, 1, 0, 1, 0, 0, 1}}
>>
>> or
>>
>> Clear[f]
>> f[a_, b_] := Boole@Positive[a - b];
>> SetAttributes[f, Listable]
>> f[Transpose@x, Transpose@y]
>>
>> {{1, 1, 0, 0, 1, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 1, 1, 0, 1, 0, 0}, {0,
>>  1, 0, 1, 0, 1, 0, 0, 0, 1}, {0, 1, 0, 0, 1, 0, 1, 0, 0, 1}}
>>
>> or
>>
>> Clear[f]
>> f[a_, b_] := Boole@Positive[a - b];
>> SetAttributes[f, Listable]
>> Transpose@f[x, y]
>>
>> {{1, 1, 0, 0, 1, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 1, 1, 0, 1, 0, 0}, {0,
>>  1, 0, 1, 0, 1, 0, 0, 0, 1}, {0, 1, 0, 0, 1, 0, 1, 0, 0, 1}}
>>
>> Bobby
>>
>> On Sat, 14 Mar 2009 05:39:39 -0500, Gregory Lypny
>> <gregory.lypny at videotron.ca> wrote:
>>
>>> Hello everyone,
>>>
>>> I'm trying to develop a modest skill in mapping functions and I've
>>> been working on this problem.
>>>
>>> Suppose I have two 100 x 4 matrices, X and Y, and I want to see
>>> whether each value in a column of X is bigger than each value in the
>>> corresponding column of Y.  In other words, compare column 1 of X with
>>> column 1 of Y, column 2 of X with column 2 of Y, and so on.
>>>
>>> It's easy to generate a 100 x 4 table of Booleans using Table as
>>>
>>> 	Table[Boole[X[[i ,  j]] > Y[[i, j]]], {i, 100}, {j, 4}]
>>>
>>> But what about without Table?  I am able to do it for the comparison
>>> of any one column as
>>>
>>> 	Boole[#[[1]] > #[[2]]] & /@ Transpose[{X[[All, ]], Y[[All, 1]]}]
>>>
>>> but I'm not sure how to extend this to other columns.  Any tip would
>>> be much appreciated.
>>>
>>> Regards,
>>>
>>> 	Gregory
>>>
>>
>>
>>
>> --DrMajorBob at bigfoot.com
>

--
DrMajorBob at bigfoot.com

• Prev by Date: Re: Re: Weird NMinimize behaviour
• Next by Date: Re: Re: Weird NMinimize behaviour
• Previous by thread: Re: Comparing Corresponding Columns of Two Matrices
• Next by thread: Re: Comparing Corresponding Columns of Two Matrices