3

I think I have seen what I was looking for a ong time ago but I can't see nothing such in documentation.

Quite often, I would like to create a Table with given bounds and number of elements, rather than calculating the appropriate step.

For example, instead of Table[f[i],{i,10,11,0.25}] I am looking for something like ...[f[i],{i,10,11,5}] where 5 corresponds to the number of elements.

Is there a built-in function/option for this?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
anderstood
  • 14,301
  • 2
  • 29
  • 80

3 Answers3

3

For anything rectangular you can use Array as in

Array[f, 5, {10., 11.}]

which yields

{f[10.],f[10.25],f[10.5],f[10.75],f[11.]}

Note: As discussed here on stack overflow Array is faster than Tablefor multidimensional lists.

Sascha
  • 8,459
  • 2
  • 32
  • 66
2

Beginning with V10.1,

Table[f[i // N], {i, Subdivide[10, 11, 4]}]

or

f /@ N[Subdivide[10, 11, 4]]

will work. Note that you must give 4 rather 5, because the last argument indicates the number of divisions not the number of elements.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
0
f /@ FindDivisions[{10, 11}, 4] // N

enter image description here

eldo
  • 67,911
  • 5
  • 60
  • 168
  • FindDivisions actually entails the mean pitfall that it chooses divisions it deems "nice" and does not necessarily do what you asked for (e.g. first and/or last entry slightly outside the defined scope). In my opinion the documentation for FindDivision should have a "Possible Issues" section – Sascha Jan 07 '16 at 17:04