MathGroup Archive 2011

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

Search the Archive

Re: Fast matrices comparison

  • To: mathgroup at smc.vnet.net
  • Subject: [mg115916] Re: Fast matrices comparison
  • From: Gabriel Landi <gtlandi at gmail.com>
  • Date: Tue, 25 Jan 2011 06:31:37 -0500 (EST)

Not sure it is the fastest way but this is what I came up with:

m1 = {{8, 7}, {3, 9}};
m2 = {{2, 3}, {4, 5}};


NonPositive[m2 - m1]                                  =>        {{True,
True}, {False, True}}

Position[NonPositive[m2 - m1], False]     =>        {{2, 1}}


ReplacePart[m1, % -> 0]




The NonPositive finds where m2-m1 is either negative or zero. The position
gives the positions in the matrix where this occurs and then ReplacePart
replaces only

the parts where NonPositive gave False.


Hope it helps.


Gabriel


On Tue, Jan 25, 2011 at 7:19 AM, JJ <wateronwildfire at gmail.com> wrote:

> Hi MathGroup
>
> 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 functions.
>
> Thank you.
>
>
>


  • Prev by Date: Re: disable default Dynamic Updating Enabled
  • Next by Date: Re: Fast matrices comparison
  • Previous by thread: Re: Fast matrices comparison
  • Next by thread: Re: Fast matrices comparison