Re: reading different locations of a matrix specified
- To: mathgroup at smc.vnet.net
- Subject: [mg113875] Re: reading different locations of a matrix specified
- From: Oliver Ruebenkoenig <ruebenko at wolfram.com>
- Date: Tue, 16 Nov 2010 06:14:49 -0500 (EST)
On Tue, 16 Nov 2010, Nasser M. Abbasi wrote: > On 11/16/2010 2:30 AM, Oliver Ruebenkoenig wrote: > >>> ------------------------------------ >>> A={{1,2,3},{4,5,6},{7,8,9}}; >>> pos={{1,1},{3,3}}; >>> >>> A[[ ##& @@ #]]& /@ pos >>> --------------------------- >>> >>> {1,9} >>> > > >> >> Extract[A, pos] >> > > I thought I should add something more as to why I ended up with the command > > A[[ Sequence @@ #]]& /@ pos > > It started like this: > > pos={1,1} > > Typing A[[ pos ]] does not work as I wanted (to get element A[[ 1,1 ]]) > And this what started the whole investigation. > > I needed a way to remove the '{ }' from around the {1,1} so > it will be A[[ 1,1 ]] > > Then I remembered Sequence does that, so I typed > > A[[Sequence@@pos]] and that is how I got to the command above. > > Now, I see that > > Extract[A,pos] to be equivalent to A[[ Sequence@@pos ]] > without the need to have to worry about '{ }' there. > > So, basically Extract[A,{1,1}] works as meant to be, but > A[[ {1,1} ]] did not. > > So, my choice will be now to use Extract[A,pos] > > so many commands, so little time :) > > --Nasser > Maybe a small addition, Extract works well with commands like Position, which return data like you have.