JC Coez wrote:
> How could I apply a function f to elements of an array which
> are selected by a given list, those elements could be modified
> several times. For example if f is 2^ and v =: i.10 and
> ind=: 3 5 3 the result would be 0 1 2 81 4 25 6 7 8 9
First off, I think you mean f=:^&2 or (equivalently) f=:*:
Second off, I prefer to avoid using single character names
like v which have special meaning to the interpreter. It actually
doesn't make any difference in this case, but likewise in this
case it doesn't hurt to use capital letters for all single
character names.
With that out of the way,
~.3 5 3
3 5
#/.~3 5 3
2 1
In other words, you can transform your "list of indices
with some of them repeated" to a "list of unique indices"
and a corresponding "list of repeat counts".
To use those repeat counts, you'll probably want to use ^:
But ^: is a conjunction, which means it doesn't have rank,
and to make this work you'll want to be using it at rank 0.
To work around that issue, you can define a helper adjective:
pow=:1 :0("0)
:
u^:x y
)
This adverb pow takes a monad and returns a rank 0 dyad
whose left argument is the number of times the monad is
applied.
Gathering all the intermediate steps, I get:
F=:*:
ind=:3 5 3
V=:i. 10
pow=:1 :0("0)
:
u^:x y
)
J=: [EMAIL PROTECTED]
G=: #/[EMAIL PROTECTED] F pow J { ]
Thus:
ind J V
3 5
ind G V
81 25
Putting them together:
ind G`J`]} V
0 1 2 81 4 25 6 7 8 9
--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm