Re: how to transform a list in a sequence of arguments?
- To: mathgroup at smc.vnet.net
- Subject: [mg77277] Re: how to transform a list in a sequence of arguments?
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Wed, 6 Jun 2007 07:10:15 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <f43i7q$3lt$1@smc.vnet.net>
alexxx.magni at gmail.com wrote:
> I believed it was fairly trivial, yet I was unable to find a
> solution ...
>
> say I have a 3d matrix,
>
> In[64]:= s
> Dimensions[s]
>
> Out[64]= {{{-1, 1, 1}, {-1, 1, 1}, {-1, 1, 1}}, {{1, 1, 1}, {1, 1, 1},
> {1, -1, -1}}, {{1, -1, 1}, {-1, 1, -1}, {-1, 1, -1}}}
> Out[65]= {3, 3, 3}
>
> and I want to access&modify it in a procedure, in which the indices
> i,j,k are found sequentially: {1,1,1}, {1,1,2}, ....
>
> How can I do it, since s[[{i,j,k}]] is not the same - of course - of
> s[[i,j,k]] ???
>
> I finally wrote the construct:
>
> Apply[s[[##]] &, {i,j,k}]
>
> ant this works OK in reading, but not in writing since it reports:
>
> s[[##]] & @@ {1,2,1} = "0"
> Set::write: Tag Apply in (s[[##1]]&)@@{1,2,1} is Protected. >>
>
> any idea on how to make it work read/write???
>
> P.S.
> it <does> work, for now, because I'm using this (where i0 is the
> indices list):
> s = ReplacePart[s, i0 -> - Extract[s, i0]];
>
> but I fear that it must be much slower than a Part[] access
>
>
> Thank you!
>
> Alessandro Magni
Alessandro,
The following should work as you want.
In[1]:=
s = {{{-1, 1, 1}, {-1, 1, 1}, {-1, 1, 1}},
{{1, 1, 1}, {1, 1, 1}, {1, -1, -1}},
{{1, -1, 1}, {-1, 1, -1}, {-1, 1, -1}}};
s[[##]] & @@ {1, 2, 1}
Set[s[[##]], 0] & @@ {1, 2, 1}
s
Out[2]= -1
Out[3]= 0
Out[4]= {{{-1, 1, 1}, {0, 1, 1}, {-1, 1, 1}}, {{1, 1, 1}, {1, 1,
1}, {1, -1, -1}}, {{1, -1, 1}, {-1, 1, -1}, {-1, 1, -1}}}
Regards,
Jean-Marc