How can I replace non-reals in a multidimensional list?
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
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!
list-manipulation pattern-matching replacement
add a comment |Â
up vote
2
down vote
favorite
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!
list-manipulation pattern-matching replacement
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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!
list-manipulation pattern-matching replacement
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!
list-manipulation pattern-matching replacement
list-manipulation pattern-matching replacement
edited 3 hours ago
Carl Woll
57.5k273149
57.5k273149
asked 3 hours ago
Jmeeks29ig
46228
46228
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
You had two issues.
- You can't use
Unequal
("!=") withAlternatives
("|"). - 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
Great, thanks! That helps me to understand it a lot better
â Jmeeks29ig
1 hour ago
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
You had two issues.
- You can't use
Unequal
("!=") withAlternatives
("|"). - 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
Great, thanks! That helps me to understand it a lot better
â Jmeeks29ig
1 hour ago
add a comment |Â
up vote
4
down vote
accepted
You had two issues.
- You can't use
Unequal
("!=") withAlternatives
("|"). - 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
Great, thanks! That helps me to understand it a lot better
â Jmeeks29ig
1 hour ago
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
You had two issues.
- You can't use
Unequal
("!=") withAlternatives
("|"). - 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
You had two issues.
- You can't use
Unequal
("!=") withAlternatives
("|"). - 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
answered 3 hours ago
Carl Woll
57.5k273149
57.5k273149
Great, thanks! That helps me to understand it a lot better
â Jmeeks29ig
1 hour ago
add a comment |Â
Great, thanks! That helps me to understand it a lot better
â Jmeeks29ig
1 hour ago
Great, thanks! That helps me to understand it a lot better
â Jmeeks29ig
1 hour ago
Great, thanks! That helps me to understand it a lot better
â Jmeeks29ig
1 hour ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f182213%2fhow-can-i-replace-non-reals-in-a-multidimensional-list%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password