MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Comparing Corresponding Columns of Two Matrices

  • To: mathgroup at smc.vnet.net
  • Subject: [mg97535] Re: Comparing Corresponding Columns of Two Matrices
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Sat, 14 Mar 2009 18:16:19 -0500 (EST)

On 3/14/09 at 5:39 AM, gregory.lypny at videotron.ca (Gregory Lypny)
wrote:

>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.

While this particular problem could be solved using Map or
MapThread, there is much simpler and more efficient way to get
the desired result

Unitize[Sign[x -y] +1]

will return a matrix of 1's and 0's. Element i,j will be 1 if
y[[i,j]]<x[[i,j]] and 1 other wise

But if you want to use something similar to Map the same result
can be obtained as follows:

MapThread[Unitize[Sign[#1-#2]+1]&,{x,y}]

Or if you prefer to use Boole and Map the same result can be
obtained as:

Transpose@
  Map[Boole[GreaterEqual @@ #] &, Transpose[{x, y}, {3, 2, 1}], {2}]

  To verify the above:

  In[61]:= x = RandomInteger[{0, 10}, {5, 5}];
y = RandomInteger[{0, 10}, {5, 5}];

In[63]:= Unitize[Sign[x - y] + 1] ==
  Transpose@
   Map[Boole[GreaterEqual @@ #] &, Transpose[{x, y}, {3, 2, 1}], {2}]

Out[63]= True

In[64]:= Unitize[Sign[x - y] + 1] ==
  MapThread[Unitize[Sign[#1 - #2] + 1] &, {x, y}]

Out[64]= True




  • Prev by Date: Re: about mathlink connection
  • Next by Date: Reading in an ASCII file written by a FORTRAN program
  • Previous by thread: Re: Comparing Corresponding Columns of Two Matrices
  • Next by thread: Re: Comparing Corresponding Columns of Two Matrices