# A better way than using nested For-loops to make a search list

I'm generating a List that can be up to 26 values long, a simplified example is below. Is there a better way than nested loops?

 For[r = minr, r <= maxr, r = r + dr,
point[[1, 1]] = r;
For[theta = 0, theta <= Pi, theta = theta + dthe,
point[[1, 2]] = theta;
For[phi = -Pi, phi <= Pi, phi = phi + dphi,
point[[1, 3]] = phi;
val = myfunction[point]]]]


This really gets slow when I get ups to 5 nested loops.

• It's great that you included your code, but could you also explain what you are trying to do? It's unclear why you're nesting loops at all, since the calculations inside the inside inner loops don't seem to reference the values in the outer loops. Furthermore, it seems like you keep assigning to point[[1, 1]] different values. So what exactly are you trying to do here? – march Nov 13 '15 at 20:58
• As commented by march, please clarify your specific problem and add additional details to highlight exactly what you need as an end result. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question and then edit your question to make it better. – rhermans Nov 13 '15 at 21:05
• It is the start of a grid search/error space mapping for a test function. I'm stepping through my solution space and will record the lowest and highest error at each radius step. – Phymonkey Nov 13 '15 at 21:27
• So you are trying to maximize or minimize some function? – bill s Nov 13 '15 at 21:29
• minimize, but the function is really nasty (takes about 3 hours with the minimization functions in Mathematica), I trying to get a quick estimate of the solution space to narrow down the domain. – Phymonkey Nov 13 '15 at 21:33

Table should do the trick.

Table[myfunction[{r, theta, phi}],
{r, minr, maxr, dr},
{theta, 0, π, dthe},
{phi, -π, π, dphi}
]


This will produce a nested list of myfunction values. You may need to use Flatten on the result depending upon the shape that you want for the output.

• Thanks I will give it a try as soon as can get logged in. – Phymonkey Nov 14 '15 at 18:59
• Unfortunately, Mathematica wont let me us a list for the iterators part, and I would be stuck hard coding the Table. And how far out can you nest with Table? – Phymonkey Nov 15 '15 at 1:22
• @Phymonkey - Mathematica supports using a list as an iterator. For example, Table[i, {i, {4, 7, 12}}] produces {4, 7, 12}. – Jack LaVigne Nov 16 '15 at 2:42
• Ok, I was trying it a little different. – Phymonkey Nov 16 '15 at 16:31

Jack,I want thank you for your answer, it didn't give what I wanted but it was in the right direction. Here is the solution I have (one Do loop and a recursive function call) I could have done it with just one function but this was easier to debug, here are the functions:

Do[loop++; map[[loop]] = tablefunction[r, 2, inc, dim, range],
{r, range[[1, 1]], range[[1, 2]], inc[]}]

tablefunction[list_, index_, incr_, dim_, range_] :=
Module[{map, nlist},
If[index == 2,
nlist =
Table[{list, ang}, {ang, range[[index, 1]], range[[index, 2]],
incr[[index]]}],
nlist = Partition[
Flatten[Table[{list[[i]], ang}, {i, 1, Length[list]}, {ang,
range[[index, 1]], range[[index, 2]],
incr[[index]]}]], {index}]];
If[index != dim,
map = exploremse[nlist, index + 1, incr, dim, range],
map = calculatemse[nlist]]; map];

It is about 4 times faster than the for loops but it will eat memory if dim gets over 5.