MathGroup Archive 2011

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

Search the Archive

Re: Fast matrices comparison

  • To: mathgroup at
  • Subject: [mg115934] Re: Fast matrices comparison
  • From: Bill Rowe <readnews at>
  • Date: Wed, 26 Jan 2011 05:06:17 -0500 (EST)

On 1/25/11 at 4:19 AM, wateronwildfire at (JJ) wrote:

>I have to compare two big matrices (in the SparseArray form), say m1
>and m2. I need that the elements of m1 will be replaced by 0 if they
>are less or equal to the corresponding elements of m2.

>For example, if m1={{8,7},{3,9}} and m2={{2,3},{4,5}}, I would like
>to have m1={{8,7},{0,9}} because m1[[2,1]]<m2[[2,1]].

>I can do it easily with two nested for loops but I think it is not
>an efficient way to do it. I was wondering if somebody knows how to
>do it using a fast conditional replacement rule and/or pure

Here is one way to do what you want that does not use any
explicit loops and should be fairly fast:

In[6]:= Unitize[Sign[m1 - m2] + 1] m1

Out[6]= {{8, 7}, {0, 9}}

  • Prev by Date: Re: Need help with Mathematica
  • Next by Date: Re: Vector problem
  • Previous by thread: Re: Fast matrices comparison
  • Next by thread: Re: Fast matrices comparison