Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #9034
| From | roby <roby.nowak@gmail.com> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: Map onto a column |
| Date | 2012-03-14 05:48 +0000 |
| Organization | Steven M. Christensen and Associates, Inc and MathTensor, Inc. |
| Message-ID | <jjpbel$pad$1@smc.vnet.net> (permalink) |
| References | <jjmv20$cfr$1@smc.vnet.net> |
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
Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web