3
$\begingroup$

Ok. I'm obviously not understanding something about pattern matching.

I have a list called t like the following:

t = {{3.2,5.9,Indeterminate},{4.5,-3 - 100 (1 - 2/(Underflow[] + 1)) + 200 (1 - Underflow[]),7.9},{1.2,9.1,2.3}}

Where I would like to use ReplaceAll[] to convert all non-real values to 0.0, like so:

{{3.2,5.9,0.0},{4.5,0.0,7.9},{1.2,9.1,2.3}}

How could I do this with ReplaceAll[] and pattern matching?

I have tried:

t /. r_ /; Head[r] != List | Real -> 0.0

But that does nothing to the list. I am trying to wrap my head around pattern matching, but I'm not quite there yet, and any help would be greatly appreciated.

Thanks!

P.S.- I would really like to understand this in context of pattern matching, but other more efficient ways of doing this are also appreciated!

$\endgroup$
6
$\begingroup$

You had two issues.

  1. You can't use Unequal ("!=") with Alternatives ("|").
  2. Even if you correct your pattern, the pattern would match the whole expression.

Using Replace with a level spec and a corrected pattern:

t = {{3.2,5.9,Indeterminate},{4.5,-3-100 (1-2/(Underflow[]+1))+200 (1-Underflow[]),7.9},{1.2,9.1,2.3}};

Replace[
    t,
    r_ /; !MatchQ[Head[r], List | Real] -> 0.0,
    {2}
]

{{3.2, 5.9, 0.}, {4.5, 0., 7.9}, {1.2, 9.1, 2.3}}

Another possibility:

Replace[
    t,
    Except[_?NumberQ] -> 0.,
    {2}
]

{{3.2, 5.9, 0.}, {4.5, 0., 7.9}, {1.2, 9.1, 2.3}}

$\endgroup$
  • $\begingroup$ Great, thanks! That helps me to understand it a lot better $\endgroup$ – Jmeeks29ig Sep 20 '18 at 1:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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