Re: reading different locations of a matrix specified
- To: mathgroup at smc.vnet.net
- Subject: [mg113877] Re: reading different locations of a matrix specified
- From: Sebastian Schmitt <sschmitt at physi.uni-heidelberg.de>
- Date: Wed, 17 Nov 2010 05:27:00 -0500 (EST)
Hi Nasser! 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? I use Extract: In[6]:= A = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} Out[6]= {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} In[8]:= pos = {{1, 1}, {3, 3}}; In[9]:= Extract[A, pos] Out[9]= {1, 9} Cheers, Sebastian