Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Re: Re: Two questions about modification of a matrix.


On 23 Jun 2009, at 20:03, Leonid Shifrin wrote:

>
> For the second problem, probably the easiest way to speed up code in  
> case
> of large matrices is to use Compile:
>
> nullifyNegativeCompiled =
>   Compile[{{matr, _Real, 2}},
>   Module[{i = 1, j = 1, new = matr},
>    For[i = 1, i <= Length[matr], i++,
>     For[j = 1, j <= Length[matr], j++,
>      If[matr[[i, j]] < 0, new[[i, j]] = 0]]];
>    new]];
>
> My benchmarks indicate 3-fold speed - up on <largeTestMatr>, with  
> respect to
> a rule-based solution.

In my tests the following is both simpler and much faster :

ClipNullify[mat_] := Clip[#, {0, Infinity}] & /@ mat


For example:

mat = RandomInteger[{-100, 100}, {1000, 1000}];
(m1 = nullifyNegativeCompiled[mat]); // Timing
{0.320822, Null}
(m2 = ClipNullify[mat]); // Timing
{0.013523, Null}

m1 == m2
True


Andrzej Kozlowski


  • Prev by Date: Re: Matrix construction
  • Next by Date: Re: For loops with mathematica....
  • Previous by thread: Re: Two questions about modification of a matrix.
  • Next by thread: Re: Re: Two questions about modification of