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

*To*: mathgroup at smc.vnet.net*Subject*: [mg101096] Re: [mg101057] Re: [mg101041] Two questions about modification of a matrix.*From*: Andrzej Kozlowski <akoz at mimuw.edu.pl>*Date*: Wed, 24 Jun 2009 06:29:40 -0400 (EDT)*References*: <200906220822.EAA09140@smc.vnet.net> <200906231103.HAA07999@smc.vnet.net>

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

**References**:**Two questions about modification of a matrix.***From:*Mayasky <alix.zhang@gmail.com>

**Re: Two questions about modification of a matrix.***From:*Leonid Shifrin <lshifr@gmail.com>