Re: Comparing Corresponding Columns of Two Matrices
- To: mathgroup at smc.vnet.net
- Subject: [mg97547] Re: [mg97496] Comparing Corresponding Columns of Two Matrices
- From: DrMajorBob <btreat1 at austin.rr.com>
- Date: Sat, 14 Mar 2009 18:18:30 -0500 (EST)
- References: <200903141039.FAA12977@smc.vnet.net>
- Reply-to: drmajorbob at bigfoot.com
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}}
Map[Boole, Thread /@ Thread[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
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
- References:
- Comparing Corresponding Columns of Two Matrices
- From: Gregory Lypny <gregory.lypny@videotron.ca>
- Comparing Corresponding Columns of Two Matrices