How to populate a table with results from a while function

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
5
down vote

favorite
1












Does anybody know how to save the intermediate results inside a while function in a table?
Here is my code. I would like to save the x,y,counter values in a table in every step that while takes. Thank you!



counter = 0;
While[counter < 5,
counter = counter + 1;
sol = NSolve[2 x + y - counter == 0,
3 y + 5 x - 2 counter == 0, x, y];
]









share|improve this question

























    up vote
    5
    down vote

    favorite
    1












    Does anybody know how to save the intermediate results inside a while function in a table?
    Here is my code. I would like to save the x,y,counter values in a table in every step that while takes. Thank you!



    counter = 0;
    While[counter < 5,
    counter = counter + 1;
    sol = NSolve[2 x + y - counter == 0,
    3 y + 5 x - 2 counter == 0, x, y];
    ]









    share|improve this question























      up vote
      5
      down vote

      favorite
      1









      up vote
      5
      down vote

      favorite
      1






      1





      Does anybody know how to save the intermediate results inside a while function in a table?
      Here is my code. I would like to save the x,y,counter values in a table in every step that while takes. Thank you!



      counter = 0;
      While[counter < 5,
      counter = counter + 1;
      sol = NSolve[2 x + y - counter == 0,
      3 y + 5 x - 2 counter == 0, x, y];
      ]









      share|improve this question













      Does anybody know how to save the intermediate results inside a while function in a table?
      Here is my code. I would like to save the x,y,counter values in a table in every step that while takes. Thank you!



      counter = 0;
      While[counter < 5,
      counter = counter + 1;
      sol = NSolve[2 x + y - counter == 0,
      3 y + 5 x - 2 counter == 0, x, y];
      ]






      replacement table infinite-loop






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 8 at 9:20









      harazogo

      483




      483




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          counter = 0;
          sol = ;
          While[counter < 5, counter = counter + 1;
          AppendTo[sol, Flatten[counter, x, y /.
          NSolve[2 x + y - counter == 0, 3 y + 5 x - 2 counter == 0, x, y]]]]

          sol



          1, 1., -1., 2, 2., -2., 3, 3., -3., 4, 4., -4., 5, 5., -5.




          or



          counter = 0;
          sol2 = ConstantArray[0, 5];
          While[counter < 5, counter = counter + 1;
          sol2[[counter]] = Flatten@counter, x, y /.
          NSolve[2 x + y - counter == 0, 3 y + 5 x - 2 counter == 0, x, y]]
          sol2



          1, 1., -1., 2, 2., -2., 3, 3., -3., 4, 4., -4., 5, 5., -5.







          share|improve this answer





























            up vote
            7
            down vote













            Use Sow and Reap :



            counter = 0;
            Reap @ While[counter < 5, counter = counter + 1;
            sol = NSolve[2 x + y - counter == 0,
            3 y + 5 x - 2 counter == 0, x, y];
            Sow[x, y, counter /. sol]]





            share|improve this answer



























              up vote
              4
              down vote













              I think other answer take your question too literally. You do not really need While. IMHO Mathematica way of solving this is to use Table.



              Assuming, that your equation always gives a single solution:



              Table[
              x, y, counter /.
              First@NSolve[2 x + y - counter == 0,
              3 y + 5 x - 2 counter == 0, x, y], counter, 5] // TableForm


              enter image description here






              share|improve this answer





























                up vote
                3
                down vote













                To reach the same end, as you mentioned Table (a digression: if you know Python, you can compare Wolfram's Table with its list comprehensions, which is attached with more conciseness and readability.), the most direct method is just to use it:



                Table[
                Append[
                NSolve[2 x + y - counter == 0, 3 y + 5 x - 2 counter == 0, x, y],
                counter
                ],
                counter, 4
                ]




                x -> 1., y -> -1., 1, x -> 2., y -> -2., 2,
                x -> 3., y -> -3., 3, x -> 4., y -> -4., 4







                share|improve this answer


















                • 2




                  Using Append you abuse the list of solutions from NSolve. I would make a NSolve,counter instead of Append[NSolve,counter].
                  – Johu
                  Sep 8 at 11:44










                Your Answer




                StackExchange.ifUsing("editor", function ()
                return StackExchange.using("mathjaxEditing", function ()
                StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
                StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
                );
                );
                , "mathjax-editing");

                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "387"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                convertImagesToLinks: false,
                noModals: false,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                 

                draft saved


                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181480%2fhow-to-populate-a-table-with-results-from-a-while-function%23new-answer', 'question_page');

                );

                Post as a guest






























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                2
                down vote



                accepted










                counter = 0;
                sol = ;
                While[counter < 5, counter = counter + 1;
                AppendTo[sol, Flatten[counter, x, y /.
                NSolve[2 x + y - counter == 0, 3 y + 5 x - 2 counter == 0, x, y]]]]

                sol



                1, 1., -1., 2, 2., -2., 3, 3., -3., 4, 4., -4., 5, 5., -5.




                or



                counter = 0;
                sol2 = ConstantArray[0, 5];
                While[counter < 5, counter = counter + 1;
                sol2[[counter]] = Flatten@counter, x, y /.
                NSolve[2 x + y - counter == 0, 3 y + 5 x - 2 counter == 0, x, y]]
                sol2



                1, 1., -1., 2, 2., -2., 3, 3., -3., 4, 4., -4., 5, 5., -5.







                share|improve this answer


























                  up vote
                  2
                  down vote



                  accepted










                  counter = 0;
                  sol = ;
                  While[counter < 5, counter = counter + 1;
                  AppendTo[sol, Flatten[counter, x, y /.
                  NSolve[2 x + y - counter == 0, 3 y + 5 x - 2 counter == 0, x, y]]]]

                  sol



                  1, 1., -1., 2, 2., -2., 3, 3., -3., 4, 4., -4., 5, 5., -5.




                  or



                  counter = 0;
                  sol2 = ConstantArray[0, 5];
                  While[counter < 5, counter = counter + 1;
                  sol2[[counter]] = Flatten@counter, x, y /.
                  NSolve[2 x + y - counter == 0, 3 y + 5 x - 2 counter == 0, x, y]]
                  sol2



                  1, 1., -1., 2, 2., -2., 3, 3., -3., 4, 4., -4., 5, 5., -5.







                  share|improve this answer
























                    up vote
                    2
                    down vote



                    accepted







                    up vote
                    2
                    down vote



                    accepted






                    counter = 0;
                    sol = ;
                    While[counter < 5, counter = counter + 1;
                    AppendTo[sol, Flatten[counter, x, y /.
                    NSolve[2 x + y - counter == 0, 3 y + 5 x - 2 counter == 0, x, y]]]]

                    sol



                    1, 1., -1., 2, 2., -2., 3, 3., -3., 4, 4., -4., 5, 5., -5.




                    or



                    counter = 0;
                    sol2 = ConstantArray[0, 5];
                    While[counter < 5, counter = counter + 1;
                    sol2[[counter]] = Flatten@counter, x, y /.
                    NSolve[2 x + y - counter == 0, 3 y + 5 x - 2 counter == 0, x, y]]
                    sol2



                    1, 1., -1., 2, 2., -2., 3, 3., -3., 4, 4., -4., 5, 5., -5.







                    share|improve this answer














                    counter = 0;
                    sol = ;
                    While[counter < 5, counter = counter + 1;
                    AppendTo[sol, Flatten[counter, x, y /.
                    NSolve[2 x + y - counter == 0, 3 y + 5 x - 2 counter == 0, x, y]]]]

                    sol



                    1, 1., -1., 2, 2., -2., 3, 3., -3., 4, 4., -4., 5, 5., -5.




                    or



                    counter = 0;
                    sol2 = ConstantArray[0, 5];
                    While[counter < 5, counter = counter + 1;
                    sol2[[counter]] = Flatten@counter, x, y /.
                    NSolve[2 x + y - counter == 0, 3 y + 5 x - 2 counter == 0, x, y]]
                    sol2



                    1, 1., -1., 2, 2., -2., 3, 3., -3., 4, 4., -4., 5, 5., -5.








                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Sep 8 at 10:07

























                    answered Sep 8 at 9:24









                    kglr

                    161k8185384




                    161k8185384




















                        up vote
                        7
                        down vote













                        Use Sow and Reap :



                        counter = 0;
                        Reap @ While[counter < 5, counter = counter + 1;
                        sol = NSolve[2 x + y - counter == 0,
                        3 y + 5 x - 2 counter == 0, x, y];
                        Sow[x, y, counter /. sol]]





                        share|improve this answer
























                          up vote
                          7
                          down vote













                          Use Sow and Reap :



                          counter = 0;
                          Reap @ While[counter < 5, counter = counter + 1;
                          sol = NSolve[2 x + y - counter == 0,
                          3 y + 5 x - 2 counter == 0, x, y];
                          Sow[x, y, counter /. sol]]





                          share|improve this answer






















                            up vote
                            7
                            down vote










                            up vote
                            7
                            down vote









                            Use Sow and Reap :



                            counter = 0;
                            Reap @ While[counter < 5, counter = counter + 1;
                            sol = NSolve[2 x + y - counter == 0,
                            3 y + 5 x - 2 counter == 0, x, y];
                            Sow[x, y, counter /. sol]]





                            share|improve this answer












                            Use Sow and Reap :



                            counter = 0;
                            Reap @ While[counter < 5, counter = counter + 1;
                            sol = NSolve[2 x + y - counter == 0,
                            3 y + 5 x - 2 counter == 0, x, y];
                            Sow[x, y, counter /. sol]]






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Sep 8 at 9:27









                            LouisB

                            4,1541616




                            4,1541616




















                                up vote
                                4
                                down vote













                                I think other answer take your question too literally. You do not really need While. IMHO Mathematica way of solving this is to use Table.



                                Assuming, that your equation always gives a single solution:



                                Table[
                                x, y, counter /.
                                First@NSolve[2 x + y - counter == 0,
                                3 y + 5 x - 2 counter == 0, x, y], counter, 5] // TableForm


                                enter image description here






                                share|improve this answer


























                                  up vote
                                  4
                                  down vote













                                  I think other answer take your question too literally. You do not really need While. IMHO Mathematica way of solving this is to use Table.



                                  Assuming, that your equation always gives a single solution:



                                  Table[
                                  x, y, counter /.
                                  First@NSolve[2 x + y - counter == 0,
                                  3 y + 5 x - 2 counter == 0, x, y], counter, 5] // TableForm


                                  enter image description here






                                  share|improve this answer
























                                    up vote
                                    4
                                    down vote










                                    up vote
                                    4
                                    down vote









                                    I think other answer take your question too literally. You do not really need While. IMHO Mathematica way of solving this is to use Table.



                                    Assuming, that your equation always gives a single solution:



                                    Table[
                                    x, y, counter /.
                                    First@NSolve[2 x + y - counter == 0,
                                    3 y + 5 x - 2 counter == 0, x, y], counter, 5] // TableForm


                                    enter image description here






                                    share|improve this answer














                                    I think other answer take your question too literally. You do not really need While. IMHO Mathematica way of solving this is to use Table.



                                    Assuming, that your equation always gives a single solution:



                                    Table[
                                    x, y, counter /.
                                    First@NSolve[2 x + y - counter == 0,
                                    3 y + 5 x - 2 counter == 0, x, y], counter, 5] // TableForm


                                    enter image description here







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Sep 8 at 13:42

























                                    answered Sep 8 at 11:51









                                    Johu

                                    3,3381033




                                    3,3381033




















                                        up vote
                                        3
                                        down vote













                                        To reach the same end, as you mentioned Table (a digression: if you know Python, you can compare Wolfram's Table with its list comprehensions, which is attached with more conciseness and readability.), the most direct method is just to use it:



                                        Table[
                                        Append[
                                        NSolve[2 x + y - counter == 0, 3 y + 5 x - 2 counter == 0, x, y],
                                        counter
                                        ],
                                        counter, 4
                                        ]




                                        x -> 1., y -> -1., 1, x -> 2., y -> -2., 2,
                                        x -> 3., y -> -3., 3, x -> 4., y -> -4., 4







                                        share|improve this answer


















                                        • 2




                                          Using Append you abuse the list of solutions from NSolve. I would make a NSolve,counter instead of Append[NSolve,counter].
                                          – Johu
                                          Sep 8 at 11:44














                                        up vote
                                        3
                                        down vote













                                        To reach the same end, as you mentioned Table (a digression: if you know Python, you can compare Wolfram's Table with its list comprehensions, which is attached with more conciseness and readability.), the most direct method is just to use it:



                                        Table[
                                        Append[
                                        NSolve[2 x + y - counter == 0, 3 y + 5 x - 2 counter == 0, x, y],
                                        counter
                                        ],
                                        counter, 4
                                        ]




                                        x -> 1., y -> -1., 1, x -> 2., y -> -2., 2,
                                        x -> 3., y -> -3., 3, x -> 4., y -> -4., 4







                                        share|improve this answer


















                                        • 2




                                          Using Append you abuse the list of solutions from NSolve. I would make a NSolve,counter instead of Append[NSolve,counter].
                                          – Johu
                                          Sep 8 at 11:44












                                        up vote
                                        3
                                        down vote










                                        up vote
                                        3
                                        down vote









                                        To reach the same end, as you mentioned Table (a digression: if you know Python, you can compare Wolfram's Table with its list comprehensions, which is attached with more conciseness and readability.), the most direct method is just to use it:



                                        Table[
                                        Append[
                                        NSolve[2 x + y - counter == 0, 3 y + 5 x - 2 counter == 0, x, y],
                                        counter
                                        ],
                                        counter, 4
                                        ]




                                        x -> 1., y -> -1., 1, x -> 2., y -> -2., 2,
                                        x -> 3., y -> -3., 3, x -> 4., y -> -4., 4







                                        share|improve this answer














                                        To reach the same end, as you mentioned Table (a digression: if you know Python, you can compare Wolfram's Table with its list comprehensions, which is attached with more conciseness and readability.), the most direct method is just to use it:



                                        Table[
                                        Append[
                                        NSolve[2 x + y - counter == 0, 3 y + 5 x - 2 counter == 0, x, y],
                                        counter
                                        ],
                                        counter, 4
                                        ]




                                        x -> 1., y -> -1., 1, x -> 2., y -> -2., 2,
                                        x -> 3., y -> -3., 3, x -> 4., y -> -4., 4








                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Sep 8 at 10:09

























                                        answered Sep 8 at 9:40









                                        Αλέξανδρος Ζεγγ

                                        2,016721




                                        2,016721







                                        • 2




                                          Using Append you abuse the list of solutions from NSolve. I would make a NSolve,counter instead of Append[NSolve,counter].
                                          – Johu
                                          Sep 8 at 11:44












                                        • 2




                                          Using Append you abuse the list of solutions from NSolve. I would make a NSolve,counter instead of Append[NSolve,counter].
                                          – Johu
                                          Sep 8 at 11:44







                                        2




                                        2




                                        Using Append you abuse the list of solutions from NSolve. I would make a NSolve,counter instead of Append[NSolve,counter].
                                        – Johu
                                        Sep 8 at 11:44




                                        Using Append you abuse the list of solutions from NSolve. I would make a NSolve,counter instead of Append[NSolve,counter].
                                        – Johu
                                        Sep 8 at 11:44

















                                         

                                        draft saved


                                        draft discarded















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181480%2fhow-to-populate-a-table-with-results-from-a-while-function%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        這個網誌中的熱門文章

                                        How to combine Bézier curves to a surface?

                                        Carbon dioxide

                                        Why am i infinitely getting the same tweet with the Twitter Search API?