Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #9006 > unrolled thread
| Started by | Sly Pandemia <slypandemia@gmail.com> |
|---|---|
| First post | 2012-03-13 08:04 +0000 |
| Last post | 2012-03-16 11:35 +0000 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Map onto a column Sly Pandemia <slypandemia@gmail.com> - 2012-03-13 08:04 +0000
Re: Map onto a column Ray Koopman <koopman@sfu.ca> - 2012-03-14 05:45 +0000
Re: Map onto a column JasonE <ebaughjason@gmail.com> - 2012-03-14 05:47 +0000
Re: Map onto a column roby <roby.nowak@gmail.com> - 2012-03-14 05:48 +0000
Re: Map onto a column dr DanW <dmaxwarren@gmail.com> - 2012-03-16 11:35 +0000
| From | Sly Pandemia <slypandemia@gmail.com> |
|---|---|
| Date | 2012-03-13 08:04 +0000 |
| Subject | Map onto a column |
| Message-ID | <jjmv20$cfr$1@smc.vnet.net> |
Is there a neat way to map a function onto a specific column in an
array?
For example if I have
a = {{1,2,3},{4,5,6},{7,8,9}}
and I want to map f onto the second element in each row. I would
normally use
b = Map[MapAt[f,#,2]&,a]
but this seems a bit awkward. I would expect to be able to use
b = MapAt[f,a,{All,2}]
but this appears to be illegal syntax.
Is there any way to do what I want with a single Map/MapAt function?
[toc] | [next] | [standalone]
| From | Ray Koopman <koopman@sfu.ca> |
|---|---|
| Date | 2012-03-14 05:45 +0000 |
| Message-ID | <jjpb9p$p9j$1@smc.vnet.net> |
| In reply to | #9006 |
On Mar 13, 1:04 am, Sly Pandemia <slypande...@gmail.com> wrote:
> Is there a neat way to map a function onto a specific column in an
> array?
>
> For example if I have
>
> a = {{1,2,3},{4,5,6},{7,8,9}}
>
> and I want to map f onto the second element in each row. I would
> normally use
>
> b = Map[MapAt[f,#,2]&,a]
>
> but this seems a bit awkward. I would expect to be able to use
>
> b = MapAt[f,a,{All,2}]
>
> but this appears to be illegal syntax.
>
> Is there any way to do what I want with a single Map/MapAt function?
When I started using Mathematica I avoided abbreviations such as
& /@ @ @@ @@@ /; /. //. etc, but now I prefer them to the long forms
in most situations. Here is your normal way, written more compactly,
which IMHO makes it not awkward:
b = MapAt[f,#,2]&/@a
The remaining methods work only if f is Listable;
this step can be omitted if f is already Listable:
SetAttributes[f,Listable]
If a has many rows then this can be much faster than your normal way:
b = Transpose@MapAt[f,Transpose@a,2]
(From time to time I've been tempted to put T=Transpose in my init.m,
so I could write things like T@MapAt[f,T@a,2].)
This changes column 2 of a in place and returns the new column 2:
a[[All,2] = f@a[[All,2]]
This sets b and returns the new column 2:
b = a; b[[All,2]] = f@b[[All,2]]
[toc] | [prev] | [next] | [standalone]
| From | JasonE <ebaughjason@gmail.com> |
|---|---|
| Date | 2012-03-14 05:47 +0000 |
| Message-ID | <jjpbdm$pa4$1@smc.vnet.net> |
| In reply to | #9006 |
I don't know if this is *neat*, but it works:
In[88]:= Map[f,Transpose[a][[2]]]
Out[88]= {f[2],f[5],f[8]}
Jason
Jason Ebaugh
Contact: jasonebaugh@jasonebaugh.info
LinkedIn: www.linkedin.com/in/jasonebaugh
[toc] | [prev] | [next] | [standalone]
| From | roby <roby.nowak@gmail.com> |
|---|---|
| Date | 2012-03-14 05:48 +0000 |
| Message-ID | <jjpbel$pad$1@smc.vnet.net> |
| In reply to | #9006 |
On 13 Mrz., 09:04, Sly Pandemia <slypande...@gmail.com> wrote:
> Is there a neat way to map a function onto a specific column in an
> array?
>
> For example if I have
>
> a = {{1,2,3},{4,5,6},{7,8,9}}
>
> and I want to map f onto the second element in each row. I would
> normally use
>
> b = Map[MapAt[f,#,2]&,a]
>
> but this seems a bit awkward. I would expect to be able to use
>
> b = MapAt[f,a,{All,2}]
>
> but this appears to be illegal syntax.
>
> Is there any way to do what I want with a single Map/MapAt function?
... well a single MapAt[ ] but at the cost of two Transpose[ ]
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} //
Transpose // MapAt[f /@ # &, #, 2] & // Transpose
or a single MapIndexed[ ] but some matching
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} //
MapIndexed[If[MatchQ[#2, {_, 2}], f@#, #] &, #, {2}] &
but all in all not better than your solution
my best effort:
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} //
ReplacePart[#, {v_, 2} :> f@#[[v, 2]]] &
Robert
[toc] | [prev] | [next] | [standalone]
| From | dr DanW <dmaxwarren@gmail.com> |
|---|---|
| Date | 2012-03-16 11:35 +0000 |
| Message-ID | <jjv8ir$phs$1@smc.vnet.net> |
| In reply to | #9006 |
MapAt needs a level specification. I posted this a few years ago, but here it is again:
MapAtLevel::usage = "MapAtLevel[\!\(\*
StyleBox[\"f\", \"TI\"]\),\!\(\*
StyleBox[\"expr\", \"TI\"]\),\!\(\*
StyleBox[\"n\", \"TI\"]\),\!\(\*
StyleBox[\"levelspec\", \"TI\"]\)] applies \!\(\*
StyleBox[\"f\", \"TI\"]\) to the element at position \!\(\*
StyleBox[\"n\", \"TI\"]\) at level \!\(\*
StyleBox[\"levelspec\", \"TI\"]\) in \!\(\*
StyleBox[\"expr\", \"TI\"]\)."
MapAtLevel[f_, expr_, n_, levelspec_: {1}] :=
Map[MapAt[f, #, n] &, expr, levelspec - 1]
I have this in a package of useful utilities I load in my init.m.
A typical use is when I want to plot data on a Decibel scale:
data = Table[{i, 10^(10.*(i/20))}, {i, 5}];
ListPlot[MapAtLevel[20*Log10[#1] & , data, 2, {2}], Joined -> True]
Enjoy,
Daniel
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web