# Get position from one list and choose element of another list in Compile

How can do it?

t = {};
f =
Compile[{{l, _Integer}},
Module[
{list = {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}},
Map[AppendTo[t, list[[#]] &], l]];
t
];


CompiledFunction::cfta

there is two related question: this and this.

Update: with help of Sumit (here) I write another function but still there is problem with Sequence@@.

Compile[{{l, _Integer}},
Module[{positions = {1, 2, 4}},
Table[KroneckerDelta[#1,
Sequence @@ Flatten@Position[list, #2]]*#2 &[x, y],
{x,positions}, {y, l}]
]
]


Or this:

  f = Compile[{{r, _Integer, 2}},
Table[
Map[KroneckerDelta[p, Position[r, #][[1]]]*# &, r], {p, {2, 4}}]]

• You're practicing to use Compile? If not, why not simply use t = list[[l]] ? Jan 13 '16 at 7:45
• Yes, Of course , there is not any problem without compile.@xzczd Jan 13 '16 at 8:24
• the simpler question is : How to choose some elements of given list to compile by their position. Jan 13 '16 at 9:00
• this might be helpful mathematica.stackexchange.com/questions/88527/… Jan 13 '16 at 10:04
• Then are you just looking for a compiled function or you want to insist on AppendTo, Map etc.? If the former, then how about f = Compile[{{l, _Integer, 1}}, Module[{list = {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}}, list[[l]]]]; t = f[{1, 2, 1}]? Jan 13 '16 at 11:42

You are using the global symbol t within a compiled function. This is not going to work like you expect it. What you probably want is the following simple function providing that I understood you correctly:

f = Compile[{{l, _Integer, 1}}, Module[{
list = {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}},
t = Most[{{1}}]},
t = Map[list[[#]] &, l];
t
]
];


No AppendTo needed and it gives you a local t that you can use inside the compiled function itself.

f[{1, 2, 1, 3}]
(* {{1, 1, 1}, {2, 2, 2}, {1, 1, 1}, {3, 3, 3}} *)


Please pay attention that you have to help Compile determining the correct data types. I explicitly defined l as a list of numbers and even the expression

Most[{{1}}]


only gives the empty list {}, BUT the compiler saw that I had a list of integer lists and therefore, assumes t should have exactly this type.

• Actually if one insists on AppendTo then Map will fail, not sure about the exact reason though. Jan 13 '16 at 15:55
• I have another question but I don't know ask it as a new question or not, actually the list that I want to choose from it is a global variable. I try it to give the list to Compile (function) but it produce another problem, the function should be used in FixedPoint. but I don't know how to give 2 arguments function to FixedPoint. Jan 13 '16 at 17:08
• Ask it as new question and give your short algorithm without using Compile, so that people understand what you try to do. Jan 13 '16 at 17:12
• and another problem: the elements of former list are not in same size. for example {{1},{1,3,4,2},{4,2}} Jan 13 '16 at 17:12
• This is called a ragged array and it cannot be used as natural inside Compile as you might think. There are ways to solve this, but that are advanced and you already need to be quite confident with Compile to use them. In general, just think of it as impossible to either give ragged arrays as input or return them. Jan 13 '16 at 17:16