Matlab逻辑运算实例用法详解

4
(4)

逻辑运算符,一般包括逻辑与、逻辑或、逻辑非、逻辑异或;Matlab逻辑运算中,也大致包括这些逻辑运算。接下来,我们分别介绍下几种常见的逻辑运算。

  • 逻辑与:两个操作数同时为1时,结果为1,否则为0;符号:&或and
  • 逻辑或:两个操作数同时为0时,结果为0,否则为1;|或or
  • 逻辑非:当操作数为0时,结果为1,否则为0;~
  • 逻辑异或:两个操作结果相同时,结果为0,否则为1;xor
  • 有非零元素为真;any
  • 所有元素均非零则为真;all
Matlab逻辑运算实例用法详解

接下来,我们举几个例子说明它们的用法:

>> A=1;
>> B=2;
>> C=0;
>> A&B
ans =
     1
>> A&B&C
ans =
     0
>> A|B
ans =
     1
>> A|B|C
ans =
     1
>> ~A
ans =
     0
>> ~C
ans =
     1
>> xor(A,B)
ans =
     0
>> xor(B,C)
ans =
     1
 
>> any([A,B,C])
ans =
     1
>> all([A,B,C])
ans =
     0

需要注意的是,与、或、非、异或,都可以直接对元素操作,而any和all只能对矩阵操作,所以我们要用([])这种方式进行运算。最后,再给出Matlab中关于这些函数的帮助文档如下:

>> help and
 &  Logical and.
    A & B performs a logical and of arrays A and B and returns an array
    containing elements set to either logical 1 (TRUE) or logical 0
    (FALSE). An element of the output array is set to 1 if both input
    arrays contain a non-zero element at that same array location.
    Otherwise, that element is set to 0.  A and B must have the same 
    dimensions unless one is a scalar.  
 
    C = and(A,B) is called for the syntax 'A & B' when A or B is an object.
 
    Note that there are two logical and operators in MATLAB. The & operator
    performs an element-by-element and between matrices, while the &&
    operator performs a short-circuit and between scalar values. See the
    documentation for details.
>> help or
 |   Logical or.
    A | B performs a logical or of arrays A and B and returns an array
    containing elements set to either logical 1 (TRUE) or logical 0
    (FALSE). An element of the output array is set to 1 if either input
    array contains a non-zero element at that same array location.
    Otherwise, that element is set to 0.  A and B must have the same 
    dimensions unless one is a scalar.  
 
    C = or(A,B) is called for the syntax 'A | B' when A or B is an object.
 
    Note that there are two logical or operators in MATLAB. The | operator
    performs an element-by-element or between matrices, while the ||
    operator performs a short-circuit or between scalar values. See the
    documentation for details.
 
>> help ~
 ~   Logical not.
    ~A performs a logical not of input array A, and returns an array
    containing elements set to either logical 1 (TRUE) or logical 0 (FALSE).
    An element of the output array is set to 1 if A contains a zero value
    element at that same array location.  Otherwise, that element is set to
    0.
 
    B = not(A) is called for the syntax '~A' when A is an object.
 
    ~ can also be used to ignore input arguments in a function definition,
    and output arguments in a function call.  See "help punct"
>> help xor
 xor Logical EXCLUSIVE OR.
    xor(S,T) is the logical symmetric difference of elements S and T.
    The result is logical 1 (TRUE) where either S or T, but not both, is 
    nonzero.  The result is logical 0 (FALSE) where S and T are both zero 
    or nonzero.  S and T must have the same dimensions (or one can be a 
    scalar).
 
>> help any
 any    True if any element of a vector is a nonzero number or is
    logical 1 (TRUE).  any ignores entries that are NaN (Not a Number).
 
    For vectors, any(V) returns logical 1 (TRUE) if any of the 
    elements of the vector is a nonzero number or is logical 1 (TRUE).
    Otherwise it returns logical 0 (FALSE).  For matrices, any(X) 
    operates on the columns of X, returning a row vector of logical 1's 
    and 0's.  For multi-dimensional arrays, any(X) operates on the 
    first non-singleton dimension.
 
    any(X,DIM) works down the dimension DIM.  For example, any(X,1)
    works down the first dimension (the rows) of X.
 
>> help all
 all    True if all elements of a vector are nonzero.
    For vectors, all(V) returns logical 1 (TRUE) if none of the elements 
    of the vector are zero.  Otherwise it returns logical 0 (FALSE).  For 
    matrices, all(X) operates on the columns of X, returning a row vector
    of logical 1's and 0's. For N-D arrays, all(X) operates on the first
    non-singleton dimension.
 
    all(X,DIM) works down the dimension DIM.  For example, all(X,1)
    works down the first dimension (the rows) of X.

需要注意的是,任何非0元素都为真!

共计4人评分,平均4

到目前为止还没有投票~

很抱歉,这篇文章对您没有用!

让我们改善这篇文章!

告诉我们我们如何改善这篇文章?

原创文章,作者:古哥,转载需经过作者授权同意,并附上原文链接:https://iymark.com/articles/573.html

(0)
微信公众号
古哥的头像古哥管理团队
上一篇 2020年10月25日 20:13
下一篇 2020年10月26日 19:58

你可能感兴趣的文章

发表回复

登录后才能评论
微信小程序
微信公众号