Replacing negative components of a matrix with zeros [closed]

How can I replace the negative components of a matrix with zeros?

=================

=================

1 Answer
1

=================

Gathering the answers given in the comments.

• Clip:

Clip[m, {0, Infinity}]

• ReplaceAll:

m /. _?Negative -> 0

• Replace:

Replace[m, _?Negative -> 0, {-1}]

• UnitStep:

m UnitStep[m]

• HeavisideTheta:

m HeavisideTheta[m]

• Map and Max:

Map[Max[0, #] &, m, {-1}]

Example:

SeedRandom[30]
m = RandomInteger[{-10, 10}, {3, 2, 2}]

(* {{{-6, 7}, {0, -7}}, {{-3, 1}, {-5, 10}}, {{3, 4}, {-4, 0}}} *)

Clip[m, {0, Infinity}]

(* {{{0, 7}, {0, 0}}, {{0, 1}, {0, 10}}, {{3, 4}, {0, 0}}} *)

SameQ[Clip[m, {0, Infinity}],
m /. _?Negative -> 0,
Replace[m, _?Negative -> 0, {-1}],
m UnitStep[m],
m HeavisideTheta[m],
Map[Max[0, #] &, m, {-1}]
]

(* True *)

1

 

One more: Map[Max[0, #] &, m, {-1}].
– J. M.♦
Mar 25 at 2:14

1

 

Replace[m, _?Negative -> 0, -1]
– garej
Mar 25 at 6:03