Re: reading different locations of a matrix specified by indices
- To: mathgroup at smc.vnet.net
- Subject: [mg113858] Re: reading different locations of a matrix specified by indices
- From: "Nasser M. Abbasi" <nma at 12000.org>
- Date: Tue, 16 Nov 2010 05:04:43 -0500 (EST)
- Reply-to: nma at 12000.org
On 11/15/2010 3:28 PM, Nasser M. Abbasi wrote: > Given a matrix A, and list of locations withing the matrix, > where each location is given by {i,j} entry, I wanted to > find the most efficient and clear way to obtain the entries > in the matrix by these locations. > > I wanted to ask the experts here what they think of this > method, and if they can suggest a better way. > > Example, given > > A={{1,2,3}, > {4,5,6}, > {7,8,9}} > > and wanted to obtain the entries at say {1,1} and {3,3}, > which will be 1 and 9 in this example. So I first made a > list of the locations : > > pos={ {1,1},{3,3}}; > > Then typed > > Map[ A[[Sequence@@ # ]]& , pos ] > > and got back > > {1,9} > > So, this seems to work. > > Any suggestion of may be of a more 'obvious' or better way? > While looking around, I Found one can use ##& in place of Sequence ------------------------------------ A={{1,2,3},{4,5,6},{7,8,9}}; pos={{1,1},{3,3}}; A[[ ##& @@ #]]& /@ pos --------------------------- {1,9} --Nasser