3
$\begingroup$

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[3];
init = ConstantArray[0, 4];
final = Block[{inter = init}, Table[
    Echo[inter];
    Echo[{index}];
    inter[[index]] = RandomChoice[{-2, -1, 1, 2}], {index, 
     Reverse@Range[4]}]];
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[4], 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[4] is replaced by RandomSample@Range[4] 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.

$\endgroup$
4
  • 1
    $\begingroup$ 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[[4]], index[[3]], index[[2]], index[[1]]}. So final is the reverse of index $\endgroup$ – Bob Hanlon Dec 24 '20 at 1:26
  • $\begingroup$ That makes sense. Is there an easy way of saving final as a copy of inter that is being modified within the Table? $\endgroup$ – anon248 Dec 24 '20 at 1:42
  • 1
    $\begingroup$ There is no need for final. You apparently want inter; so use RandomSeed[3]; inter = ConstantArray[0, 4]; Table[inter[[index]] = RandomChoice[{-2, -1, 1, 2}], {index, Reverse@Range[4]}]; inter $\endgroup$ – Bob Hanlon Dec 24 '20 at 1:55
  • $\begingroup$ 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. $\endgroup$ – anon248 Dec 24 '20 at 5:04
4
$\begingroup$

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[4]}];
final

output

{1, 4, 9, 16}
$\endgroup$
3
  • $\begingroup$ 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? $\endgroup$ – anon248 Dec 24 '20 at 2:18
  • 2
    $\begingroup$ @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. $\endgroup$ – m_goldberg Dec 24 '20 at 2:52
  • $\begingroup$ 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. $\endgroup$ – anon248 Dec 24 '20 at 5:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.