# Is there a cleaner/shorter way to select-replace in matrices? [duplicate]

I love Mathematica, but I find it oddly lacking when it comes to how matrices are handled. Here is an example of code that works but seems like it should be a lot shorter (and minus the loop).

I’m a super-newbie though so maybe one of you smart people has an idea for making this nicer. The point is to take all elements of the matrix that evaluate as True for some criterion (here, being less than 7) and re-assign them to a new value.

testmat = {{-1, 1, -3}, {10, 11, 5}, {15, -2, 7}};
MatrixForm[testmat]
badpos = Position[testmat, _?(# < 7 &)]; {nend, trash} = Dimensions[badpos]; For[i = 1, i <= nend, i++, selpos = badpos[[i]]; p1 = selpos[[1]]; p2 = selpos[[2]]; testmat[[p1, p2]] = 0; ] MatrixForm[testmat] ================= ================= 1 Answer 1 ================= You can Replace at level 2: Replace[testmat, x_ /; x < 7 -> 0, {2}]

An alternative, MATLAB-style vectorized solution is

UnitStep[testmat – 7] testmat

This second approach is beneficial for performance in certain cases (but not always). In this case it is going to be considerably faster for very large matrices.

It can be simplified to

BoolEval[testmat >= 7] testmat

using this package.

Thanks everyone. I’m very impressed with the terseness possible if you know the commands! I apologize for the duplicate, I DID search but a lot of times when you are very ignorant it’s hard just to hit the right terms, I guess.
– ETM
Mar 18 ’13 at 18:47

@ETM Don’t worry, sometimes I remember that there is a duplicate but I still cannot find it easily … this time I didn’t even remember even though I posted a answer in the duplicate question as well.
– Szabolcs
Mar 18 ’13 at 18:51

@Szabolcs In any case, it’s better to post an answer twice than a question … twice
– Dr. belisarius
Mar 18 ’13 at 19:41