Why comparing matrices is not evaluated as boolean in Octave?











up vote
0
down vote

favorite












Im new to Octave and playing around with the console.



why when comparing matrices, the expression is not evaluates as boolean :



example:



>> A=[1,2;3,4];     % creating 2x2 matrix
>> 5 == 5 % sample comparison returns true (1)
ans = 1

>> A(1,1) == A(1,1) % single element comparison returns true (1)
ans = 1

>> A == A % returns 2x2 matrix ???
ans =

1 1
1 1

>> size(A == A) % prove that the above returns 2x2 matrix
ans =

2 2









share|improve this question




























    up vote
    0
    down vote

    favorite












    Im new to Octave and playing around with the console.



    why when comparing matrices, the expression is not evaluates as boolean :



    example:



    >> A=[1,2;3,4];     % creating 2x2 matrix
    >> 5 == 5 % sample comparison returns true (1)
    ans = 1

    >> A(1,1) == A(1,1) % single element comparison returns true (1)
    ans = 1

    >> A == A % returns 2x2 matrix ???
    ans =

    1 1
    1 1

    >> size(A == A) % prove that the above returns 2x2 matrix
    ans =

    2 2









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Im new to Octave and playing around with the console.



      why when comparing matrices, the expression is not evaluates as boolean :



      example:



      >> A=[1,2;3,4];     % creating 2x2 matrix
      >> 5 == 5 % sample comparison returns true (1)
      ans = 1

      >> A(1,1) == A(1,1) % single element comparison returns true (1)
      ans = 1

      >> A == A % returns 2x2 matrix ???
      ans =

      1 1
      1 1

      >> size(A == A) % prove that the above returns 2x2 matrix
      ans =

      2 2









      share|improve this question















      Im new to Octave and playing around with the console.



      why when comparing matrices, the expression is not evaluates as boolean :



      example:



      >> A=[1,2;3,4];     % creating 2x2 matrix
      >> 5 == 5 % sample comparison returns true (1)
      ans = 1

      >> A(1,1) == A(1,1) % single element comparison returns true (1)
      ans = 1

      >> A == A % returns 2x2 matrix ???
      ans =

      1 1
      1 1

      >> size(A == A) % prove that the above returns 2x2 matrix
      ans =

      2 2






      arrays matlab matrix octave






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 23:15









      Luis Mendo

      92.2k1054120




      92.2k1054120










      asked Nov 11 at 19:50









      chenchuk

      1,96231322




      1,96231322
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          == is for element-wise comparison of two matrices. To check whether two matrices are same or not, use isequal.






          share|improve this answer




























            up vote
            0
            down vote













            Sardar's answer is correct, but when it comes to computational time, I think that my alternative answer is better: You can as well check that all elements of the boolean matrix A == A are 1, i.e., that the sum of 1s in the matrix A==A equals the number of elements of A, i.e:





            sum((A == A)(:)) == numel(A)



            ans = 1





            Where the operator (:) simply vectorizes the matrix A==A so that it can be added with sum(). Compare the two answers when you matrix is quite big, for instance by defining A = rand(1e4), the computation time is considerably different...






            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%2f53252570%2fwhy-comparing-matrices-is-not-evaluated-as-boolean-in-octave%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
              4
              down vote



              accepted










              == is for element-wise comparison of two matrices. To check whether two matrices are same or not, use isequal.






              share|improve this answer

























                up vote
                4
                down vote



                accepted










                == is for element-wise comparison of two matrices. To check whether two matrices are same or not, use isequal.






                share|improve this answer























                  up vote
                  4
                  down vote



                  accepted







                  up vote
                  4
                  down vote



                  accepted






                  == is for element-wise comparison of two matrices. To check whether two matrices are same or not, use isequal.






                  share|improve this answer












                  == is for element-wise comparison of two matrices. To check whether two matrices are same or not, use isequal.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 19:55









                  Sardar Usama

                  14.4k82244




                  14.4k82244
























                      up vote
                      0
                      down vote













                      Sardar's answer is correct, but when it comes to computational time, I think that my alternative answer is better: You can as well check that all elements of the boolean matrix A == A are 1, i.e., that the sum of 1s in the matrix A==A equals the number of elements of A, i.e:





                      sum((A == A)(:)) == numel(A)



                      ans = 1





                      Where the operator (:) simply vectorizes the matrix A==A so that it can be added with sum(). Compare the two answers when you matrix is quite big, for instance by defining A = rand(1e4), the computation time is considerably different...






                      share|improve this answer

























                        up vote
                        0
                        down vote













                        Sardar's answer is correct, but when it comes to computational time, I think that my alternative answer is better: You can as well check that all elements of the boolean matrix A == A are 1, i.e., that the sum of 1s in the matrix A==A equals the number of elements of A, i.e:





                        sum((A == A)(:)) == numel(A)



                        ans = 1





                        Where the operator (:) simply vectorizes the matrix A==A so that it can be added with sum(). Compare the two answers when you matrix is quite big, for instance by defining A = rand(1e4), the computation time is considerably different...






                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          Sardar's answer is correct, but when it comes to computational time, I think that my alternative answer is better: You can as well check that all elements of the boolean matrix A == A are 1, i.e., that the sum of 1s in the matrix A==A equals the number of elements of A, i.e:





                          sum((A == A)(:)) == numel(A)



                          ans = 1





                          Where the operator (:) simply vectorizes the matrix A==A so that it can be added with sum(). Compare the two answers when you matrix is quite big, for instance by defining A = rand(1e4), the computation time is considerably different...






                          share|improve this answer












                          Sardar's answer is correct, but when it comes to computational time, I think that my alternative answer is better: You can as well check that all elements of the boolean matrix A == A are 1, i.e., that the sum of 1s in the matrix A==A equals the number of elements of A, i.e:





                          sum((A == A)(:)) == numel(A)



                          ans = 1





                          Where the operator (:) simply vectorizes the matrix A==A so that it can be added with sum(). Compare the two answers when you matrix is quite big, for instance by defining A = rand(1e4), the computation time is considerably different...







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 29 at 9:59









                          nukimov

                          415




                          415






























                              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%2f53252570%2fwhy-comparing-matrices-is-not-evaluated-as-boolean-in-octave%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

                              The Sandy Post

                              Danny Elfman

                              Pages that link to "Head v. Amoskeag Manufacturing Co."