MathGroup Archive 2009

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

Search the Archive

Re: Re: Mapping to a specific column or row in a matrix

  • To: mathgroup at smc.vnet.net
  • Subject: [mg103305] Re: [mg103287] Re: Mapping to a specific column or row in a matrix
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Mon, 14 Sep 2009 07:11:44 -0400 (EDT)
  • References: <h7qem5$us$1@smc.vnet.net> <h8an76$h86$1@smc.vnet.net>

Hi John,

On Sun, Sep 13, 2009 at 5:01 AM, John Jowett <john.m.jowett at gmail.com>wrote:

> Here are a few other methods which work:
>
> I'm not sure why this method works.  Perhaps someone can clarify?
>
> mat = ConstantArray[1, {10, 5}];;
> mat[[All, 3]] = mat[[All, 3]]/10;
> mat
>
>
Your code uses the special case of the extended functionality of Part, which
Vince and myself also used (somewhat more generally) in our posts earlier in
this thread. It works because <mat> is an L-value (can be modified
in-place), and because Part has this extended functionality to make multiple
assignments to regular (sub)structures simultaneously. The side effect of
execution of your code is that <mat> is modified. If this is not desirable,
one can use a local variable (like I did In my version),

Regards,
Leonid




> In this method (which I use a lot), the rule depends on the structure
> and its contents
>
> mat = ConstantArray[1, {10, 5}];
> mat = mat /. {a_, b_, x_Integer, c__} :> {a, b, x/10, c}
>
>
> This method feels safe but I don't like it and it may be inefficient:
>
> mat = ConstantArray[1, {10, 5}];
> Do[mat = ReplacePart[mat, {k, 3} -> mat[[k, 3]]/10], {k, 1, 10}];
> mat
>
> John Jowett
>
>
>
> On Sep 10, 1:16 pm, dr DanW <dmaxwar... at gmail.com> wrote:
> > You are correct that you need a combination of Map and MapAt.
> > Specifically, you need the position spec from MapAt with the level
> > spec from Map.
> >
> > MapAtLevel[f_, expr_, n_, levelspec_: {1}] :=
> >  Map[MapAt[f, #, n] &, expr, levelspec - 1]
> >
> > It looks like you did this, but called it clunky.  By creating a new
> > function, it becomes much less clunky.  I often have to plot data from
> > a list of {x,y} pairs, but I want to transform all the y values to
> > decibel:
> >
> > dat = {{1, 200.}, {2, 250.}, {3, 300.}};
> >
> > In[30]:= MapAtLevel[20 Log[10, #] &, dat, 2, 2]
> >
> > Out[30]= {{1, 46.0206}, {2, 47.9588}, {3, 49.5424}}
> >
> > Daniel
>
>
>


  • Prev by Date: Re: Re: Changing style of autogenerated messages in Mathematica 7
  • Next by Date: Two Axis DateListPlot
  • Previous by thread: Re: Mapping to a specific column or row in a matrix
  • Next by thread: Newbie questions on Font in notebook