为什么比较矩阵在 Octave 中不被评估为布尔值?

Why comparing matrices is not evaluated as boolean in Octave?

我是 Octave 的新手,正在使用控制台。

为什么比较矩阵时,表达式的计算结果不是布尔值:

示例:

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

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

Sardar 的答案是正确的,但是在计算时间上,我认为我的替代答案更好:您也可以检查布尔矩阵 A == A 的所有元素是否为 1,即矩阵A==A中1的和等于A的元素个数,即:

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

ans = 1

其中运算符 (:) 只是将矩阵 A==A 向量化,以便它可以与 sum() 相加。当你的矩阵很大时比较这两个答案,比如定义A = rand(1e4),计算时间有很大不同...