Re: reading different locations of a matrix specified by indices
- To: mathgroup at smc.vnet.net
- Subject: [mg113871] Re: reading different locations of a matrix specified by indices
- From: Raffy <adraffy at gmail.com>
- Date: Tue, 16 Nov 2010 06:14:03 -0500 (EST)
- References: <ibtkvu$5io$1@smc.vnet.net>
On Nov 16, 2:04 am, "Nasser M. Abbasi" <n... at 12000.org> wrote: > 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 Extract[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, {{1, 1}, {3, 3}}] === {1, = 9}