# Hard time creating arrays with For/Do

So far this code Prints just a sequence of numbers, as you can see in the "Print"-Line.

I would like to export the results in one excel sheet, which has rows "b,l,m,d,p,best n, value" and in each column i have one result line. How can I do that?

I think I have to first write a list instead of Print, which I append to each other to create a table. Then I export that table. Unfortuantely I am not able to create the lists, tables and export them even after reading the documentation.

B[n_, b_, l_, m_, d_, p_] := l+m-d-p+n
For[b = 1, b < 3, b++;
For[l = 5, l < 7, l++;
For[m = 3, m < 5, m++;
For[d = 3, d < 5, d++;
For[p = 2, p < 3, p++;
If[NMaxValue[{B[n, b, l, m, d, p], n >= 1},
n \[Element] Integers] <= 0, Print[b, l, m, d, p, 0, 0] ,   (**)

Print[b, l, m, d, p,
NArgMax[{B[n, b, l, m, d, p], n >= 1}, n \[Element] Integers],
NMaxValue[{B[n, b, l, m, d, p], n \[Element] Integers, n >= 1},
n]]]]]]]]

• Thx all. Got it. How can i mark as solved? Apr 26 '17 at 13:13
• You can click on the check mark of the answer of Gustavo in order to accept his answer. Apr 26 '17 at 14:19
• did you actually want the numbers run together as your Print does? Apr 26 '17 at 18:11

You are clearly a beginner. Experienced Mathematica users run away in panic when presented so many For loops. Using Do is better than nesting For and Table is better. In general you never Print something you want to export.

Table[
If[
NMaxValue[{B[n, b, l, m, d, p], n >= 1}, n ∈ Integers] <= 0
,  ToString /@ {b, l, m, d, p, 0, 0}
,  ToString /@ {b, l, m, d, p
, NArgMax[{B[n, b, l, m, d, p], n >= 1}, n ∈ Integers]
, NMaxValue[{B[n, b, l, m, d, p], n ∈ Integers, n >= 1}, n]
}
]
, {b, 2, 3}, {l, 6, 7}, {m, 4, 5}, {d, 4, 5}, {p, 3, 3}
]

{{{{{{2,6,4,4,3,9,12.}},{{2,6,4,5,3,9,11.}}},{{{2,6,5,4,3,9,13.}},{{2,6,5,5,3,9,12.}}}},{{{{2,7,4,4,3,9,13.}},{{2,7,4,5,3,9,12.}}},{{{2,7,5,4,3,9,14.}},{{2,7,5,5,3,9,13.}}}}},{{{{{3,6,4,4,3,9,12.}},{{3,6,4,5,3,9,11.}}},{{{3,6,5,4,3,9,13.}},{{3,6,5,5,3,9,12.}}}},{{{{3,7,4,4,3,9,13.}},{{3,7,4,5,3,9,12.}}},{{{3,7,5,4,3,9,14.}},{{3,7,5,5,3,9,13.}}}}}}

Flatten[%, 4]

{{2,6,4,4,3,9,12.},{2,6,4,5,3,9,11.},{2,6,5,4,3,9,13.},{2,6,5,5,3,9,12.},{2,7,4,4,3,9,13.},{2,7,4,5,3,9,12.},{2,7,5,4,3,9,14.},{2,7,5,5,3,9,13.},{3,6,4,4,3,9,12.},{3,6,4,5,3,9,11.},{3,6,5,4,3,9,13.},{3,6,5,5,3,9,12.},{3,7,4,4,3,9,13.},{3,7,4,5,3,9,12.},{3,7,5,4,3,9,14.},{3,7,5,5,3,9,13.}}

toExcel = StringJoin /@ %

{ "26443912.", "26453911.", "26543913.", "26553912.","27443913.","27453912.", "27543914.","27553913.","36443912.", "36453911.","36543913.","36553912.", "37443913.","37453912.","37543914.","37553913."}

Export["file.xls", toExcel]


another approach:

B[n_, b_, l_, m_, d_, p_] := l + m - d - p + n
{##, If[(amax =
NArgMax[{B[n, Sequence@##], n >= 1}, Element[n, Integers]]) >
0, Sequence @@ {amax, B[amax, Sequence@##]},
Sequence @@ {0, 0}]} & @@@
Tuples[{
Range[2, 3], Range[6, 7], Range[4, 5],
Range[4, 5], Range[3, 3]}]
Export["test.xlsx", %]

• What's the purpose of Sequence in Sequence@## ? Apr 30 '17 at 16:38