Note: In my code I used j
(ranging from 3 to 7) but I want j
to be general
I tried this
i = 0;
For[j = 3, j < 10, j++,
q[[i++]] = 2 + j]
q
Error: Symbol q in part assignment does not have an immediate value
I also tried initializing q
i = 0;
q = {0, 0, 0, 0, 0, 0, 0};
For[j = 3, j < 10, j++,
q[[i++]] = 2 + j]
q
Output: 5[6, 7, 8, 9, 10, 11, 0]
I didn't understand why 5 is outside in 5[6, 7, 8, 9, 10, 11, 0]
.In output, q doesn't look like Table.
Ok well!! while writing this I was checking suggestions and could solve it, I tried
i = 0;
q = {};
For[j = 3, j < 10, j++,
temp = 2 + j;
AppendTo[q, temp];]
q
Is this a good way to do or are there even simple methods to code?
q = Range[5, 11]
. See here for how to use loops in Mathematica (Hint: don't.). Also see here for how to avoid common pitfalls in MMA. Also: the weirdness in your second example is caused by usingi++
. Try++i
instead. $\endgroup$