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? :/
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$For
loop..Flatten@MapThread[{#1, MapThread[ {##} &, {#2, #3}]} &, {first, Transpose[second], Transpose[third]}, 1]
$\endgroup$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$