0

I was wondering why it seems not possible to put functions into a list using a for loop like below:

    K = Table[{}, {2}];
    For[i = 1, i <= 2, i++,
      S = SmoothKernelDistribution[RandomReal[1, 10]];
      Print[PDF[S, 0.5]];
      K[[i]] = Function[x, PDF[S, x]];
    ]

Although S changes, the list K does contain twice the same function. (It was simplified for 2, usually I have thousands of functions in the loop and S is much more complex.)

JHT
  • 1,005
  • 8
  • 16
  • 1
  • use Table insead of For e.g. k = Table[.... 2. don't use single capital letters. 3. you need to use With[{s =s},.. trick because Function is HoldAll. Read more in linked duplicate.
  • – Kuba Jun 21 '17 at 15:02
  • 1
    Thank you! k = Table[ s = SmoothKernelDistribution[RandomReal[1, 10]]; With[{s = s}, PDF[s, #] &], {i, 1, 2}] works as intended. – JHT Jun 21 '17 at 15:15