0
$\begingroup$

I have a matrix (tab1) made out of lines with three elements. Occasionally some of these elements are non-numeric. In these cases, I would like to assign the value 0 to them. I managed to program such an operation when the third element (z3) is non-numeric:

 tab2 = tab1 /. {z1_, z2_, z3_} /; ! NumericQ[z3] -> {z1, z2, 0}

How to make this line of code more generic and attribute the value 0 to second (z2) and third (z3) elements that are non-numeric? Or to any non-numeric element?

thanks

$\endgroup$

1 Answer 1

5
$\begingroup$
Replace[
 {{1, a}, {b, 2}},
 Except[_?NumericQ] -> 0
 , {2}
]
{{1, 0}, {0, 2}}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.