3
$\begingroup$

I am not really sure how to post my original data here, because the Tables I am using are rather big - too big to copy them here.

I will try to describe what the problem is. I have three separated tables. First one is only a list of 60 numbers with step 2 so first=Table[i, {i, 1, 120, 2}]. Both of the other two tables are similar to following output:

second=Table[Table[j, {i, 1, 60, 1}], {j, 1, 28, 1}];
third=Table[Table[i, {i, 1, 60, 1}], {j, 1, 28, 1}];

Note: Size of the tables here is identical to my original tables, except the data is different, but that is not important.

Now what I want is to combine those three tables into one exactly this way:

First Second[1] Third[1] Second[2] Third [2] Second[3] Third[3]

0

2

4

Meaning at each value of first table I want to periodically print out values of second and third table.

Here is the code that ALMOST works (I created an empty table and called it Output):

k = 1;
i=5;
For[j = 1, 
 j <= 57, j++, {
  If[j == 1, Output[[i, j]] = first[[i + 1]], {
    Output[[i, j]] = second[[k, i]],
    j++,
    Output[[i, j]] = third[[k, i]],
    k++
    }]
  }  ]

This works for any exactly specified i. But as soon as I add another For loop:

k = 1;
For[i=1,i≤60,i++,For[j = 1, 
 j <= 57, j++, {
  If[j == 1, Output[[i, j]] = first[[i + 1]], {
    Output[[i, j]] = second[[k, i]],
    j++,
    Output[[i, j]] = third[[k, i]],
    k++
    }]
  }  ]]

Everything goes wrong and says that some elements don't exist. For example element 29. However If you use my first code (without For loop for i) everything goes smoothly for i=29).

Any ideas on what to do? :/

$\endgroup$
  • $\begingroup$ you reference table second with k as first index. second is defined with first index max 28 and k can be easily getting bigger than that. It is increased by 1 in most of the inner loop iterations. $\endgroup$ – Sjoerd C. de Vries Oct 22 '15 at 15:40
  • 1
    $\begingroup$ forget the For loop.. Flatten@MapThread[{#1, MapThread[ {##} &, {#2, #3}]} &, {first, Transpose[second], Transpose[third]}, 1] $\endgroup$ – george2079 Oct 22 '15 at 16:18
  • 2
    $\begingroup$ @george2079 I prefer Transpose[{first}~Join~Riffle[second, third]] // TableForm, which I think is doing the same thing… though I have a horrible lack of sleep so it's hard to tell. $\endgroup$ – Patrick Stevens Oct 22 '15 at 16:31
  • $\begingroup$ @george2079 Perhaps my interpretation of how the table should look like was a bit loose. But the solution that Patrick Stevens suggested works perfectly and does exactly what I want! $\endgroup$ – skrat Oct 22 '15 at 16:34
  • 1
    $\begingroup$ I'm voting to close this question as off-topic because it is too localized; i.e, it applies only to the local situation and needs of its poster and answers will not benefit others. $\endgroup$ – m_goldberg Oct 22 '15 at 17:12
6
$\begingroup$

MGoldberg wants to close this question as "too localised", but I think it's a useful lesson in the power of the built-ins.

Transpose[{first}~Join~Riffle[second, third]] // TableForm

or more clearly written,

Transpose[Join[{first}, Riffle[second, third]]] // TableForm

Riffle is a great function.

$\endgroup$
6
$\begingroup$

Why use For loops at all? Use Table.

fred = Table[{i, j}, {i, 60}, {j, 60}];
Table[{2 i - 1, fred[[i]]} // Flatten, {i, 60}]

You could probably condense it all into one line with something like MapIndexed.

$\endgroup$

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