# Strange indexing behavior by Table

Maybe I do not understand how Table works, but I find this very confusing and it took me a long time to debug because this was part of a much more complicated routine.

I am trying to build an array that keeps changing within a table with two indices (here a simpler case with a single index is shown)

RandomSeed;
init = ConstantArray[0, 4];
final = Block[{inter = init}, Table[
Echo[inter];
Echo[{index}];
inter[[index]] = RandomChoice[{-2, -1, 1, 2}], {index,
Reverse@Range}]];
final


Like the code snippet above. The key thing is that the index does not start from the usual way of counting up by Range, but here it is the reverse order.

When I echo I see that the intermediate (inter) array is being filled up in the correct order: The first random choice fills up the 4th entry of the array. But when the calculation finishes and I print final, I see that final array has the reverse order, its first element is the first operation done in the Table, namely the 4th entry of the array when seen from inside.

I find this very strange: If I would like to place an entry to the 4th position of an array, why would leaving the Table change this?

In this case it can easily be converted to the order I like by a simple Reverse but I would like to consider cases where Reverse@Range is replaced by RandomSample@Range where I don't want to do this correction because I don't care to know what RandomSample is taken in different iterations.

Maybe I have a big misunderstanding of how Table is supposed to operate but I figure this may be of general relevance.

• final is being filled with the last value of the argument to the Table, i.e. inter[[index]] with the index values {4,3,2,1} so the Table is built {index[], index[], index[], index[]}. So final is the reverse of index Dec 24, 2020 at 1:26
• That makes sense. Is there an easy way of saving final as a copy of inter that is being modified within the Table? Dec 24, 2020 at 1:42
• There is no need for final. You apparently want inter; so use RandomSeed; inter = ConstantArray[0, 4]; Table[inter[[index]] = RandomChoice[{-2, -1, 1, 2}], {index, Reverse@Range}]; inter Dec 24, 2020 at 1:55
• As I explained below, this simple example didn't turn out to reflect what I really wanted. What you suggest of course qualifies as an answer to this problem. Thank you. Dec 24, 2020 at 5:04

I think you have made things more complicated than they need be using Table, which is what is getting you into trouble. This is a case where you should use Do. Like so:

final = ConstantArray[0, 4];
Do[
Echo[final];
Echo[{index}];
final[[index]] = index^2, {index, Reverse @ Range}];
final {1, 4, 9, 16}

• I get it in this context, thanks. Maybe I stripped the problem too much while asking this question and that made the use of Table awkward. In my case, I want to define a function that applies this operation (say index^2) N times to the array. At the output, I want to see N-such arrays that are each updated according to this rule. I do not want a global array that is being modified but a function that returns a N x M matrix when an Mx1 array is fed into it. This was the justification of using a Table. Does that make any sense? Dec 24, 2020 at 2:18
• @anon248. You may well be right about your example being too simple but, we here, can only work with what you post. As to your more complicated real situation, I am fairly confident that the approach using Do can be useful if only as an inner computation to an outer iteration using Table, but can't say more without knowing more about your real problem. Dec 24, 2020 at 2:52
• I actually managed to get what I wanted by using a Do inside of a Table like you suggested. Thank you very much. Hope my question is still relevant, if for nothing else as a guide to the nuances between Table and Do. Thanks again. Dec 24, 2020 at 5:03