MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: reading different locations of a matrix specified

  • To: mathgroup at smc.vnet.net
  • Subject: [mg113894] Re: reading different locations of a matrix specified
  • From: "Nasser M. Abbasi" <nma at 12000.org>
  • Date: Wed, 17 Nov 2010 05:30:08 -0500 (EST)
  • References: <201011161004.FAA05711@smc.vnet.net> <Pine.LNX.4.63.1011160429540.6974@wopr.wolfram.com> <4CE26195.7080304@12000.org> <Pine.LNX.4.63.1011160456580.6974@wopr.wolfram.com>
  • Reply-to: nma at 12000.org

On 11/16/2010 2:57 AM, Oliver Ruebenkoenig wrote:
> 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.
>

Thanks Oliver and to all the other experts who responded.

But I thought I should add something important while it is on my mind.

I did not explain another reason why I needed to use
A[[ Sequence@@pos ]].

This was of a larger program I am working on for school project.

I needed to do an UPDATE of the matrix at these locations,
not just READ.

Extract is good, and will use it, but for reading only.

For assignment into these parts, as in suppose I wanted to
update entries in the matrix to say 0, at positions given by pos.

I was using

---------------------------
A={{1,2,3},{4,5,6},{7,8,9}};
pos={{1,1},{3,3}};

(A[[ Sequence@@ #]]=0) & / @ pos;    (* UPDATE *)

A
Out[192]= {{0,2,3},{4,5,6},{7,8,0}}
--------------------------

and this works ok. But from your reponses, I started looking
again at the documenation to see if I can find another
command like Extract, but works to do a write, not just read,
and then I found it:

------------------------------------
A={{1,2,3},{4,5,6},{7,8,9}};
pos={{1,1},{3,3}};

ReplacePart[A,pos->0]  (* UPDATE *)

Out[207]= {{0,2,3},{4,5,6},{7,8,0}}
------------------------------------

So, I am very happy now. With Extract[] to read, with ReplacePart[]
to write, I have all the tools I needed now to make my code
look better. Good bye Sequence@@, it was nice knowing you ;)

Thanks
--Nasser



  • Prev by Date: Re: Best method to break apart a data set
  • Next by Date: Re: Using MapAt with Play
  • Previous by thread: Re: reading different locations of a matrix specified by indices
  • Next by thread: Re: reading different locations of a matrix specified