5
$\begingroup$

I've a list of lists,

tl = {{True, False, m12}, {False, m22, m33}, {m32, False, True}}

and each nested list has the same length, What is the proper way to replace the occurances of all m* values, i.e., m12, m22, etc., with a particular value say x.

All I want is this result

tl = {{True, False, x}, {False, x, x}, {x, False, True}}`

Any help will be appreciated.

$\endgroup$
2
  • $\begingroup$ Is there anything other than True, False and m*? $\endgroup$
    – Kuba
    Commented Jun 7, 2014 at 19:23
  • $\begingroup$ No there are only True, False or m* $\endgroup$
    – aark
    Commented Jun 7, 2014 at 19:30

3 Answers 3

8
$\begingroup$

One rather literal (string-based) interpretation of your question:

tl = {{True, False, m12}, {False, m22, m33}, {m32, False, True}};

tl /. s_ /; StringTake[ToString[s], 1] == "m" :> X

{{True, False, X}, {False, X, X}, {X, False, True}}

$\endgroup$
0
11
$\begingroup$
tl = {{True, False, m12}, {False, m22, m33}, {m32, False, True}};

Replace[tl, Except[True | False] :> z, {2}]

(* {{True, False, z}, {False, z, z}, {z, False, True}} *)
$\endgroup$
3
$\begingroup$

In this way the True and False conditions are analyzed

tl = {{True, False, m12}, {False, m22, m33}, {m32, False, True}}; 
Map[If[ToString[#1] == "True" || ToString[#1] == "False", #1, 
   x] & , tl, {2}]

$\left( \begin{array}{ccc} \text{True} & \text{False} & x \\ \text{False} & x & x \\ x & \text{False} & \text{True} \\ \end{array} \right)$

$\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.