Re: Re: Mapping to a specific column or row in a matrix
- To: mathgroup at smc.vnet.net
- Subject: [mg103020] Re: [mg103011] Re: Mapping to a specific column or row in a matrix
- From: "Ned Lieb" <okty.gy.ned at gmail.com>
- Date: Fri, 4 Sep 2009 03:15:09 -0400 (EDT)
- Reply-to: okty.gy.ned at gmail.com
Yep, there is. Double square brackets indicate coordinates. To define a function as row x in a table defined as Table, for example, you'd write something like f=Table[[x]][All]]. I assume you can drop the same syntax into a mapping function. ------Original Message------ From: Vince To: Mathgroup Sent: Sep 3, 2009 7:56 PM Subject: [mg103020] [mg103011] Re: Mapping to a specific column or row in a matrix On Sep 3, 7:10 am, pfalloon <pfall... at gmail.com> wrote: > Hi, > I'm wondering if there is a more simple/elegant way to Map a function > to a specific row or column of a matrix. The best I can come up > requires both Map and MapAt, but it feels like there should be > something more succinct (I suspect I may be overlooking something > obvious). > > Example: suppose I want to divide entries in the 3rd column by 10. > > (* my clunky solution *) > mat = ConstantArray[1, {10,5}]; > res = Map[MapAt[#/10 &, #, 3] &, mat] > > Note that the desired output is the entire matrix, so that rules out > something like: > > mat[[All,3]]/10 > > One thing that would seem natural would be to allow "All" as an > element specification in the MapAt function. Thus, what I'm trying to > do would be expressed as: > > (* doesn't work, but would be nice *) > MapAt[#/10 &, mat, {All,3}] > > and if I wanted to do it to a specific row (say, the 3rd) it would be: > > (* doesn't work, but would be nice *) > MapAt[#/10 &, mat, {3, All}] > > Any thoughts/suggestions? > > Thanks, > Peter. Consider in-place modification, if you're working with a variable/ symbol: mat = ConstantArray[1, {10,5}]; mat[[All,3]] /= 10; or more generally: mat[[All,3]] = op @ mat[[All, 3]]; where 'op' can of course be a function of more than its target. Vince Virgilio