Happy new year mathematica gurus of stack exchange!
As I see it one of the major obstacles in getting decent at programming mathematica is that, not only do you need to learn how certain commands work, but rather that you mainly need to understand how to write your syntax. This is a typical such situation, I was hoping that someone might shine some light on how to do it.
I run this piece of code:
For[i = 1, i <= samples, i++,
AppendTo[fList,
f1[randomSeeds[[1 + (n - 1) (i - 1)]][[1]],
randomSeeds[[1 + (n - 1) (i - 1)]][[2]], 0, 0, 0, 0, 0, 0]];
]
For[i = 1, i <= samples, i++,
AppendTo[fList,
f1[randomSeeds[[1 + (n - 1) (i - 1)]][[1]],
randomSeeds[[1 + (n - 1) (i - 1)]][[2]],
randomSeeds[[2 + (n - 1) (i - 1)]][[1]],
randomSeeds[[2 + (n - 1) (i - 1)]][[2]], 0, 0, 0, 0]];
]
For[i = 1, i <= samples, i++,
AppendTo[fList,
f1[randomSeeds[[1 + (n - 1) (i - 1)]][[1]],
randomSeeds[[1 + (n - 1) (i - 1)]][[2]],
randomSeeds[[2 + (n - 1) (i - 1)]][[1]],
randomSeeds[[2 + (n - 1) (i - 1)]][[2]],
randomSeeds[[3 + (n - 1) (i - 1)]][[1]],
randomSeeds[[3 + (n - 1) (i - 1)]][[2]], 0, 0]];
]
For[i = 1, i <= samples, i++,
AppendTo[fList,
f1[randomSeeds[[1 + (n - 1) (i - 1)]][[1]],
randomSeeds[[1 + (n - 1) (i - 1)]][[2]],
randomSeeds[[2 + (n - 1) (i - 1)]][[1]],
randomSeeds[[2 + (n - 1) (i - 1)]][[2]],
randomSeeds[[3 + (n - 1) (i - 1)]][[1]],
randomSeeds[[3 + (n - 1) (i - 1)]][[2]],
randomSeeds[[4 + (n - 1) (i - 1)]][[1]],
randomSeeds[[4 + (n - 1) (i - 1)]][[2]]]];
]
As you can see most of the stuff is identical in the for loops, it's just the number of zeroes that varies in the end. In fact, I only wish to run one of these for loops at the time. Just above these for loops I specify the number of dimensions I'm working in (n in the code), for n=2 I want to run the first loop, for n=3 I want to run the second etc. At the moment I comment an uncomment the undesired parts of the code, but that seems like a very ugly solution to me.
So my question is this: is there a simple way of reducing these for copies of code into one copy? It seems to me as though this is a quite ineffective way of doing things.
Edit: Small clarification: Ideally I want something like this: I give the program "2" as input and it chooses the first for loop above etc.
Cheers, David
f[1], f[2]..., why not justf[n]? :) – Kuba Jan 04 '14 at 01:59Switchblock with all the loops inside, even though I'd never actually write that myself. – Mr.Wizard Jan 04 '14 at 02:01Switchbecause in general two pieces of code that you might want to control in this way may not be similar and so it might be more semantic to give them very different names. In that scenario you have to useSwitch. – C. E. Jan 04 '14 at 02:14