Why comparing matrices is not evaluated as boolean in Octave?

后端 未结 2 520
别那么骄傲
别那么骄傲 2021-01-26 10:01

Im new to Octave and playing around with the console.

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

example:

>>         


        
相关标签:
2条回答
  • 2021-01-26 10:25

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

    0 讨论(0)
  • 2021-01-26 10:32

    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...

    0 讨论(0)
提交回复
热议问题