Why doesn't this Scilab program of finding the determinant recursively work?











up vote
0
down vote

favorite
1












I am trying to find the determinant of an $NtimesN$ matrix. Here's my code:



clc
function determinant=take_detm(A)
order=sqrt(length(A))
disp(order)
if order==2 then
determinant=A(1,1)*A(2,2)-A(1,2)*A(2,1);

else

s=0
for i=1:order
s=s+((-1)^(i+1))*A(1,i)*take_detm(A(:,i)=);//deleting 1st row and a column in the recursive call
end
determinant=s

end
endfunction
matr=input("Enter a matrix")
printf (string(take_detm(matr)))


Here's the problem: When I run the code and input a matrix as: [1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16] the console prints 4 (the order) and the program hangs. I get that rolling blue ring of Windows 7 and after some time a message which says that Scilab 6.0.1 has stopped working. Is there any issue with the algorithm or is it something else?
PS-Beginner level










share|improve this question


























    up vote
    0
    down vote

    favorite
    1












    I am trying to find the determinant of an $NtimesN$ matrix. Here's my code:



    clc
    function determinant=take_detm(A)
    order=sqrt(length(A))
    disp(order)
    if order==2 then
    determinant=A(1,1)*A(2,2)-A(1,2)*A(2,1);

    else

    s=0
    for i=1:order
    s=s+((-1)^(i+1))*A(1,i)*take_detm(A(:,i)=);//deleting 1st row and a column in the recursive call
    end
    determinant=s

    end
    endfunction
    matr=input("Enter a matrix")
    printf (string(take_detm(matr)))


    Here's the problem: When I run the code and input a matrix as: [1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16] the console prints 4 (the order) and the program hangs. I get that rolling blue ring of Windows 7 and after some time a message which says that Scilab 6.0.1 has stopped working. Is there any issue with the algorithm or is it something else?
    PS-Beginner level










    share|improve this question
























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      I am trying to find the determinant of an $NtimesN$ matrix. Here's my code:



      clc
      function determinant=take_detm(A)
      order=sqrt(length(A))
      disp(order)
      if order==2 then
      determinant=A(1,1)*A(2,2)-A(1,2)*A(2,1);

      else

      s=0
      for i=1:order
      s=s+((-1)^(i+1))*A(1,i)*take_detm(A(:,i)=);//deleting 1st row and a column in the recursive call
      end
      determinant=s

      end
      endfunction
      matr=input("Enter a matrix")
      printf (string(take_detm(matr)))


      Here's the problem: When I run the code and input a matrix as: [1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16] the console prints 4 (the order) and the program hangs. I get that rolling blue ring of Windows 7 and after some time a message which says that Scilab 6.0.1 has stopped working. Is there any issue with the algorithm or is it something else?
      PS-Beginner level










      share|improve this question













      I am trying to find the determinant of an $NtimesN$ matrix. Here's my code:



      clc
      function determinant=take_detm(A)
      order=sqrt(length(A))
      disp(order)
      if order==2 then
      determinant=A(1,1)*A(2,2)-A(1,2)*A(2,1);

      else

      s=0
      for i=1:order
      s=s+((-1)^(i+1))*A(1,i)*take_detm(A(:,i)=);//deleting 1st row and a column in the recursive call
      end
      determinant=s

      end
      endfunction
      matr=input("Enter a matrix")
      printf (string(take_detm(matr)))


      Here's the problem: When I run the code and input a matrix as: [1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16] the console prints 4 (the order) and the program hangs. I get that rolling blue ring of Windows 7 and after some time a message which says that Scilab 6.0.1 has stopped working. Is there any issue with the algorithm or is it something else?
      PS-Beginner level







      recursion scilab determinants






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 at 13:40









      BundiBedu

      115




      115
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          The problem is due to the A(:,i)= instruction. assigning only works for a set of full rows or a set of full lines, so your instruction simply removed the ith column of the A matrix (the result being a rectangular matrix)



          You can fix the problem with



              Ai=A(2:order,[1:i-1 i+1:order])//deleting 1st row and  column i
          s=s+((-1)^(i+1))*A(1,i)*take_detm(Ai); //recursive call


          Note however that the Scilab det function is much more precise and efficient






          share|improve this answer





















          • Actually, I never understood the assignment to [ ]. I was originally trying some other method to delete columns. But it wasn't working, so I found this assignment method on YouTube (without proper explanation) and used it as a last minute resort. Any link which explains the method properly?
            – BundiBedu
            Nov 25 at 3:09










          • I wanted to try out writing the determinant algorithm from scratch just for fun, hence I didn't use the in-built function.
            – BundiBedu
            Nov 25 at 3:11




















          up vote
          0
          down vote













          Okay, so I am confident I get this, hence answering my own question.



          When you want to remove a row OR a column in Scilab, you can assign it to . The following example illustrates how this works.
          Consider the matrix



          A=[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]


          which prints in the console as



          A  = 

          1. 2. 3. 4.
          5. 6. 7. 8.
          9. 10. 11. 12.
          13. 14. 15. 16.


          If you want to remove the first row, the command is



              A(1,:)=
          A =

          5. 6. 7. 8.
          9. 10. 11. 12.
          13. 14. 15. 16.


          You simply assign the first row to the empty matrix. Similarly if you wanted to remove the 2nd column, you assign it to empty matrix:



          A(:,2)=
          A =

          5. 7. 8.
          9. 11. 12.
          13. 15. 16.


          (Note that the operation is performed on the updated matrix--i.e the 1st row removed)
          But if you want to remove the 1st row and the 2nd column, you can't write:



          A(1,2)=


          Scilab says:



          Submatrix incorrectly defined.


          Here is an alternate solution using assignment to the empty matrix:
          General idea: We perform 2 operations one by one: deleting the 1st row and then the ith column.
          Posting code for just the else part:



          else

          s=0
          first_row_removed=A //taking a backup of A and then...
          first_row_removed(1,:)= //...removing the 1st row
          for i=1:order
          column_i_removed=first_row_removed //taking a backup of the 1st-row-removed matrix...
          column_i_removed(:,i)= //... and then deleting column i
          s=s+((-1)^(i+1))*A(1,i)*take_detm(column_i_removed); //recursive call
          end
          determinant=s

          end //of else


          An important thing to note is that performing the assignment to the empty matrix makes changes in the original matrix itself. Hence for every iteration of the for loop, we must make sure that we perform remove-ith-column operation on the 1st-row-removed matrix and not on the matrix which had its ith column deleted in the previous for loop iteration.
          Hence the line



          column_i_removed=first_row_removed


          must be inside the for loop.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            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: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375891%2fwhy-doesnt-this-scilab-program-of-finding-the-determinant-recursively-work%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            The problem is due to the A(:,i)= instruction. assigning only works for a set of full rows or a set of full lines, so your instruction simply removed the ith column of the A matrix (the result being a rectangular matrix)



            You can fix the problem with



                Ai=A(2:order,[1:i-1 i+1:order])//deleting 1st row and  column i
            s=s+((-1)^(i+1))*A(1,i)*take_detm(Ai); //recursive call


            Note however that the Scilab det function is much more precise and efficient






            share|improve this answer





















            • Actually, I never understood the assignment to [ ]. I was originally trying some other method to delete columns. But it wasn't working, so I found this assignment method on YouTube (without proper explanation) and used it as a last minute resort. Any link which explains the method properly?
              – BundiBedu
              Nov 25 at 3:09










            • I wanted to try out writing the determinant algorithm from scratch just for fun, hence I didn't use the in-built function.
              – BundiBedu
              Nov 25 at 3:11

















            up vote
            0
            down vote



            accepted










            The problem is due to the A(:,i)= instruction. assigning only works for a set of full rows or a set of full lines, so your instruction simply removed the ith column of the A matrix (the result being a rectangular matrix)



            You can fix the problem with



                Ai=A(2:order,[1:i-1 i+1:order])//deleting 1st row and  column i
            s=s+((-1)^(i+1))*A(1,i)*take_detm(Ai); //recursive call


            Note however that the Scilab det function is much more precise and efficient






            share|improve this answer





















            • Actually, I never understood the assignment to [ ]. I was originally trying some other method to delete columns. But it wasn't working, so I found this assignment method on YouTube (without proper explanation) and used it as a last minute resort. Any link which explains the method properly?
              – BundiBedu
              Nov 25 at 3:09










            • I wanted to try out writing the determinant algorithm from scratch just for fun, hence I didn't use the in-built function.
              – BundiBedu
              Nov 25 at 3:11















            up vote
            0
            down vote



            accepted







            up vote
            0
            down vote



            accepted






            The problem is due to the A(:,i)= instruction. assigning only works for a set of full rows or a set of full lines, so your instruction simply removed the ith column of the A matrix (the result being a rectangular matrix)



            You can fix the problem with



                Ai=A(2:order,[1:i-1 i+1:order])//deleting 1st row and  column i
            s=s+((-1)^(i+1))*A(1,i)*take_detm(Ai); //recursive call


            Note however that the Scilab det function is much more precise and efficient






            share|improve this answer












            The problem is due to the A(:,i)= instruction. assigning only works for a set of full rows or a set of full lines, so your instruction simply removed the ith column of the A matrix (the result being a rectangular matrix)



            You can fix the problem with



                Ai=A(2:order,[1:i-1 i+1:order])//deleting 1st row and  column i
            s=s+((-1)^(i+1))*A(1,i)*take_detm(Ai); //recursive call


            Note however that the Scilab det function is much more precise and efficient







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 at 15:37









            user5694329

            25613




            25613












            • Actually, I never understood the assignment to [ ]. I was originally trying some other method to delete columns. But it wasn't working, so I found this assignment method on YouTube (without proper explanation) and used it as a last minute resort. Any link which explains the method properly?
              – BundiBedu
              Nov 25 at 3:09










            • I wanted to try out writing the determinant algorithm from scratch just for fun, hence I didn't use the in-built function.
              – BundiBedu
              Nov 25 at 3:11




















            • Actually, I never understood the assignment to [ ]. I was originally trying some other method to delete columns. But it wasn't working, so I found this assignment method on YouTube (without proper explanation) and used it as a last minute resort. Any link which explains the method properly?
              – BundiBedu
              Nov 25 at 3:09










            • I wanted to try out writing the determinant algorithm from scratch just for fun, hence I didn't use the in-built function.
              – BundiBedu
              Nov 25 at 3:11


















            Actually, I never understood the assignment to [ ]. I was originally trying some other method to delete columns. But it wasn't working, so I found this assignment method on YouTube (without proper explanation) and used it as a last minute resort. Any link which explains the method properly?
            – BundiBedu
            Nov 25 at 3:09




            Actually, I never understood the assignment to [ ]. I was originally trying some other method to delete columns. But it wasn't working, so I found this assignment method on YouTube (without proper explanation) and used it as a last minute resort. Any link which explains the method properly?
            – BundiBedu
            Nov 25 at 3:09












            I wanted to try out writing the determinant algorithm from scratch just for fun, hence I didn't use the in-built function.
            – BundiBedu
            Nov 25 at 3:11






            I wanted to try out writing the determinant algorithm from scratch just for fun, hence I didn't use the in-built function.
            – BundiBedu
            Nov 25 at 3:11














            up vote
            0
            down vote













            Okay, so I am confident I get this, hence answering my own question.



            When you want to remove a row OR a column in Scilab, you can assign it to . The following example illustrates how this works.
            Consider the matrix



            A=[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]


            which prints in the console as



            A  = 

            1. 2. 3. 4.
            5. 6. 7. 8.
            9. 10. 11. 12.
            13. 14. 15. 16.


            If you want to remove the first row, the command is



                A(1,:)=
            A =

            5. 6. 7. 8.
            9. 10. 11. 12.
            13. 14. 15. 16.


            You simply assign the first row to the empty matrix. Similarly if you wanted to remove the 2nd column, you assign it to empty matrix:



            A(:,2)=
            A =

            5. 7. 8.
            9. 11. 12.
            13. 15. 16.


            (Note that the operation is performed on the updated matrix--i.e the 1st row removed)
            But if you want to remove the 1st row and the 2nd column, you can't write:



            A(1,2)=


            Scilab says:



            Submatrix incorrectly defined.


            Here is an alternate solution using assignment to the empty matrix:
            General idea: We perform 2 operations one by one: deleting the 1st row and then the ith column.
            Posting code for just the else part:



            else

            s=0
            first_row_removed=A //taking a backup of A and then...
            first_row_removed(1,:)= //...removing the 1st row
            for i=1:order
            column_i_removed=first_row_removed //taking a backup of the 1st-row-removed matrix...
            column_i_removed(:,i)= //... and then deleting column i
            s=s+((-1)^(i+1))*A(1,i)*take_detm(column_i_removed); //recursive call
            end
            determinant=s

            end //of else


            An important thing to note is that performing the assignment to the empty matrix makes changes in the original matrix itself. Hence for every iteration of the for loop, we must make sure that we perform remove-ith-column operation on the 1st-row-removed matrix and not on the matrix which had its ith column deleted in the previous for loop iteration.
            Hence the line



            column_i_removed=first_row_removed


            must be inside the for loop.






            share|improve this answer

























              up vote
              0
              down vote













              Okay, so I am confident I get this, hence answering my own question.



              When you want to remove a row OR a column in Scilab, you can assign it to . The following example illustrates how this works.
              Consider the matrix



              A=[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]


              which prints in the console as



              A  = 

              1. 2. 3. 4.
              5. 6. 7. 8.
              9. 10. 11. 12.
              13. 14. 15. 16.


              If you want to remove the first row, the command is



                  A(1,:)=
              A =

              5. 6. 7. 8.
              9. 10. 11. 12.
              13. 14. 15. 16.


              You simply assign the first row to the empty matrix. Similarly if you wanted to remove the 2nd column, you assign it to empty matrix:



              A(:,2)=
              A =

              5. 7. 8.
              9. 11. 12.
              13. 15. 16.


              (Note that the operation is performed on the updated matrix--i.e the 1st row removed)
              But if you want to remove the 1st row and the 2nd column, you can't write:



              A(1,2)=


              Scilab says:



              Submatrix incorrectly defined.


              Here is an alternate solution using assignment to the empty matrix:
              General idea: We perform 2 operations one by one: deleting the 1st row and then the ith column.
              Posting code for just the else part:



              else

              s=0
              first_row_removed=A //taking a backup of A and then...
              first_row_removed(1,:)= //...removing the 1st row
              for i=1:order
              column_i_removed=first_row_removed //taking a backup of the 1st-row-removed matrix...
              column_i_removed(:,i)= //... and then deleting column i
              s=s+((-1)^(i+1))*A(1,i)*take_detm(column_i_removed); //recursive call
              end
              determinant=s

              end //of else


              An important thing to note is that performing the assignment to the empty matrix makes changes in the original matrix itself. Hence for every iteration of the for loop, we must make sure that we perform remove-ith-column operation on the 1st-row-removed matrix and not on the matrix which had its ith column deleted in the previous for loop iteration.
              Hence the line



              column_i_removed=first_row_removed


              must be inside the for loop.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                Okay, so I am confident I get this, hence answering my own question.



                When you want to remove a row OR a column in Scilab, you can assign it to . The following example illustrates how this works.
                Consider the matrix



                A=[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]


                which prints in the console as



                A  = 

                1. 2. 3. 4.
                5. 6. 7. 8.
                9. 10. 11. 12.
                13. 14. 15. 16.


                If you want to remove the first row, the command is



                    A(1,:)=
                A =

                5. 6. 7. 8.
                9. 10. 11. 12.
                13. 14. 15. 16.


                You simply assign the first row to the empty matrix. Similarly if you wanted to remove the 2nd column, you assign it to empty matrix:



                A(:,2)=
                A =

                5. 7. 8.
                9. 11. 12.
                13. 15. 16.


                (Note that the operation is performed on the updated matrix--i.e the 1st row removed)
                But if you want to remove the 1st row and the 2nd column, you can't write:



                A(1,2)=


                Scilab says:



                Submatrix incorrectly defined.


                Here is an alternate solution using assignment to the empty matrix:
                General idea: We perform 2 operations one by one: deleting the 1st row and then the ith column.
                Posting code for just the else part:



                else

                s=0
                first_row_removed=A //taking a backup of A and then...
                first_row_removed(1,:)= //...removing the 1st row
                for i=1:order
                column_i_removed=first_row_removed //taking a backup of the 1st-row-removed matrix...
                column_i_removed(:,i)= //... and then deleting column i
                s=s+((-1)^(i+1))*A(1,i)*take_detm(column_i_removed); //recursive call
                end
                determinant=s

                end //of else


                An important thing to note is that performing the assignment to the empty matrix makes changes in the original matrix itself. Hence for every iteration of the for loop, we must make sure that we perform remove-ith-column operation on the 1st-row-removed matrix and not on the matrix which had its ith column deleted in the previous for loop iteration.
                Hence the line



                column_i_removed=first_row_removed


                must be inside the for loop.






                share|improve this answer












                Okay, so I am confident I get this, hence answering my own question.



                When you want to remove a row OR a column in Scilab, you can assign it to . The following example illustrates how this works.
                Consider the matrix



                A=[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]


                which prints in the console as



                A  = 

                1. 2. 3. 4.
                5. 6. 7. 8.
                9. 10. 11. 12.
                13. 14. 15. 16.


                If you want to remove the first row, the command is



                    A(1,:)=
                A =

                5. 6. 7. 8.
                9. 10. 11. 12.
                13. 14. 15. 16.


                You simply assign the first row to the empty matrix. Similarly if you wanted to remove the 2nd column, you assign it to empty matrix:



                A(:,2)=
                A =

                5. 7. 8.
                9. 11. 12.
                13. 15. 16.


                (Note that the operation is performed on the updated matrix--i.e the 1st row removed)
                But if you want to remove the 1st row and the 2nd column, you can't write:



                A(1,2)=


                Scilab says:



                Submatrix incorrectly defined.


                Here is an alternate solution using assignment to the empty matrix:
                General idea: We perform 2 operations one by one: deleting the 1st row and then the ith column.
                Posting code for just the else part:



                else

                s=0
                first_row_removed=A //taking a backup of A and then...
                first_row_removed(1,:)= //...removing the 1st row
                for i=1:order
                column_i_removed=first_row_removed //taking a backup of the 1st-row-removed matrix...
                column_i_removed(:,i)= //... and then deleting column i
                s=s+((-1)^(i+1))*A(1,i)*take_detm(column_i_removed); //recursive call
                end
                determinant=s

                end //of else


                An important thing to note is that performing the assignment to the empty matrix makes changes in the original matrix itself. Hence for every iteration of the for loop, we must make sure that we perform remove-ith-column operation on the 1st-row-removed matrix and not on the matrix which had its ith column deleted in the previous for loop iteration.
                Hence the line



                column_i_removed=first_row_removed


                must be inside the for loop.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 2 at 16:44









                BundiBedu

                115




                115






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375891%2fwhy-doesnt-this-scilab-program-of-finding-the-determinant-recursively-work%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    What visual should I use to simply compare current year value vs last year in Power BI desktop

                    How to ignore python UserWarning in pytest?

                    Alexandru Averescu