Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2011

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

Search the Archive

Re: Assigning part of indexed object

  • To: mathgroup at smc.vnet.net
  • Subject: [mg119798] Re: Assigning part of indexed object
  • From: Oleksandr Rasputinov <oleksandr_rasputinov at hmamail.com>
  • Date: Thu, 23 Jun 2011 07:24:06 -0400 (EDT)
  • References: <itsjn7$8u7$1@smc.vnet.net>

Arguably Set should be a bit more intelligent about this situation.
But it is an easy fix:

Unprotect[Set];
Set[sym_[[part_]], val_] := sym = ReplacePart[sym, part -> val];
Protect[Set];

In[65] := m[1] = Range[5]; m[1][[1]] = 10
Out[66] = {10,2,3,4,5}

Bear in mind that since this relies on pattern matching, it will not
work in situations
where pattern matching is not available via the interpreter (for
example, in compiled
code).

On Jun 22, 12:29 pm, "Fabrice P. Laussy" <fabrice.lau...@n0spam-
gmail.com> wrote:
> How do you set the part of an indexed object, say:
>
> m[1] = Range[5];
>
> is my indexed object (I could have m[2], etc.). Now I
> want to do something like:
>
> m[1][[1]] = 10;
>
> with effect of having m[1] read:
>
> {10, 2, 3, 4, 5}
>
> but of course the line above evaluates to 1 = 10.
>
> This could be achieved by having a non-indexed object get
> in the way, e.g., m1=m[1], assign m1 and then return to
> m[1]=m1, but I believe there should a more elegant way.


  • Prev by Date: Automatic Equation Numbering when "save as"Latex
  • Next by Date: DSolve gives different answer to NDSolve
  • Previous by thread: Re: Assigning part of indexed object
  • Next by thread: Re: Assigning part of indexed object