MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Beginner: List Indexing and Assignment

  • To: mathgroup at smc.vnet.net
  • Subject: [mg94512] Re: Beginner: List Indexing and Assignment
  • From: Raffy <raffy at mac.com>
  • Date: Mon, 15 Dec 2008 07:45:39 -0500 (EST)
  • References: <gi2ut4$a5h$1@smc.vnet.net>

a = {{1, 2, 3}, {4, 5, 6}}

"Replace all values at level 2 in matrix a that are greater than 2
with -1"
Replace[ a, x_ /; x > 2 -> -1, {2} ]

You could, if you desire that notation, write your own helper
functions:
where[m_, p_] := Position[m, p, {2}];
HoldPattern[Set[where[m_, p_], x_]] ^:= Replace[m, p -> x, {2}];
logical[m_, p_] := Replace[m, {p -> True, _ -> False}, {2}];

where[ a, x_ /; x > 6 ] = {{2, 3}};

where[ a, x_ /; x > 2 ] = -1   ==>  {{1, 2, -1}, {-1, -1, -1}}

logical[ a, 3 ] = {{False, True, False}, {False, False, False}}


  • Prev by Date: What's the problem with Mathematica 7? Seems a bug!
  • Next by Date: Re: what am i doing wrong?
  • Previous by thread: Re: Beginner: List Indexing and Assignment
  • Next by thread: Re: Beginner: List Indexing and Assignment