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: [mg94532] Re: Beginner: List Indexing and Assignment
  • From: "michael.p.croucher at googlemail.com" <michael.p.croucher at googlemail.com>
  • Date: Mon, 15 Dec 2008 07:49:16 -0500 (EST)
  • References: <gi2ut4$a5h$1@smc.vnet.net>

Hi Chris

Here is one way to do it:

a /. x_ /; x>2 -> -1

Cheers,
Mike


On 14 Dec, 12:41, C Rose <cjr.list.alia... at me.com> wrote:
> Hi
>
> I am moving from another system to Mathematica and have a few simple ques=
tions about indexing and altering lists. I've been able to find Mathematica=
 equivalents to some of the other system's  idioms, but as a Mathematica =
neophyte they're not very elegant. I'd be very grateful if someone could te=
ll me the Mathematica equivalents---or point me to a suitable Rosetta stone=
 (Google didn't easily turn one up).
>
> In the other system, I would create a 2x3 matrix using
>
> a = [1 2 3; 4 5 6]
>
> resulting in
>
> [1 2 3]
> [4 5 6]
>
> and then assign any element of the matrix whose value is greater than 2 t=
he value -1 using
>
> a(a>2) = -1
>
> resulting in
>
> [ 1  2 -1]
> [-1 -1 -1]
>
> I can do this in Mathematica by:
>
> a = ReplacePart[a, Position[a, x_ /; x > 2] -> -1]
>
> but is there a more elegant method?
>
> Another way (in the other system) is to create a logical array:
>
> logical = a>2
>
> resulting in
>
> [0 0 1]
> [1 1 1]
>
> and I could then do
>
> a(logical) = -1
>
> again resulting in
>
> [ 1  2 -1]
> [-1 -1 -1]
>
> I have been able to approximate this in Mathematica as
>
> logical = a /. x_ /; x > 2 -> True
> (* Note, unlike above, logical contains values of True and other integers=
. *)
>
> ReplacePart[a, Position[logical, x_ /; x == True] -> -1]
>
> Is there a more elegant method in Mathematica? (Of course, 'elegant' is a=
 subjective quality; perhaps 'brevity' is a better word :-)
>
> Many thanks in advance
>
> Chris



  • Prev by Date: Re: Mathematica 7 is now available
  • Next by Date: Re: Beginner: List Indexing and Assignment
  • Previous by thread: Re: Beginner: List Indexing and Assignment
  • Next by thread: Re: Beginner: List Indexing and Assignment