2
$\begingroup$

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]]]]]]]]
$\endgroup$
3
  • 1
    $\begingroup$ Thx all. Got it. How can i mark as solved? $\endgroup$
    – user34047
    Commented Apr 26, 2017 at 13:13
  • $\begingroup$ You can click on the check mark of the answer of Gustavo in order to accept his answer. $\endgroup$ Commented Apr 26, 2017 at 14:19
  • $\begingroup$ did you actually want the numbers run together as your Print does? $\endgroup$
    – george2079
    Commented Apr 26, 2017 at 18:11

2 Answers 2

6
$\begingroup$

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.

These steps may help you:

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]
$\endgroup$
2
$\begingroup$

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", %]
$\endgroup$
1
  • $\begingroup$ What's the purpose of Sequence in Sequence@## ? $\endgroup$ Commented Apr 30, 2017 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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