主要内容

全部

确定所有数组元素是非零还是真的

描述

example

B= all(A)tests along the first array dimension ofAwhose size does not equal 1, and determines if the elements are all nonzero or logical1(真的). In practice,全部is a natural extension of the logical AND operator.

  • 如果A是一个向量,然后所有(a)returns logical1(真的) if all the elements are nonzero and returns logical0(false) if one or more elements are zero.

  • 如果Ais a nonempty matrix, then所有(a)treats the columns ofAas vectors and returns a row vector of logical1s and0s.

  • 如果Ais an empty 0-by-0 matrix, then所有(a)returns logical1(真的).

  • 如果A是一个多维数组,然后所有(a)acts along the first array dimension whose size does not equal 1 and returns an array of logical values. The size of this dimension becomes1, while the sizes of all other dimensions remain the same.

B= all(A,'all')tests over all elements ofA。This syntax is valid for MATLAB®versions R2018b and later.

example

B= all(A,dim)测试沿尺寸的元素dim。这dim输入是一个正整数标量。

example

B= all(A,Vecdim)tests elements based on the dimensions specified in the vectorVecdim。例如,如果A是矩阵,然后全部(A,[1 2])tests over all elements inA, since every element of a matrix is contained in the array slice defined by dimensions 1 and 2.

例子

collapse all

创建一个3 x-3矩阵,然后测试每列的所有非零元素。

A = [0 0 3;0 0 3;0 0 3]
A =3×30 0 3 0 0 3 0 0 3
b =所有(a)
b =1x3逻辑数组0 0 1

创建一个小数值和测试的向量,哪些值小于0.5。

a = [0.53 0.67 0.01 0.38 0.07 0.42 0.69];b =(a <0.5)
b =1x7 logical array0 0 1 1 1 1 0

这output is a vector of logical values. The全部function reduces such a vector of logical values to a single condition. In this case,B =全部(a <0.5)产生逻辑0

This makes全部特别有用if语句。

如果全部(a <0.5)

%do something

else

%做其他事情

end

这code is executed depending on a single condition, rather than a vector of possibly conflicting conditions.

Create a 3-by-7-by-5 multidimensional array and test to see if all of its elements are less than 3.

a = rand(3,7,5) * 5;b = all(a(:) <3)
b =logical0

您还可以测试大于零的元素的数组。

b =全部(A(:) > 0)
b =logical1

这syntax一个(:)turns the elements ofAinto a single column vector, so you can use this type of statement on an array of any size.

Create a 3-by-3 matrix.

A = [0 0 3;0 0 3;0 0 3]
A =3×30 0 3 0 0 3 0 0 3

Test the rows ofA对于所有非零元素,通过指定dim = 2

B =全部(A,2)
b =3x1 logical array0 0 0

Create a 3-D array and determine if all elements in each page of data (rows and columns) are zero.

A(:,:,1) = [2 1; 3 5]; A(:,:,2) = [0 0; 0 0]; A(:,:,3) = [-2 9; 4 1]; B = all(A,[1 2])
b =1x1x3逻辑数组B(:,:,1) = 1 B(:,:,2) = 0 B(:,:,3) = 1

Input Arguments

collapse all

Input array, specified as a scalar, vector, matrix, or multidimensional array.

Data Types:single|double|int8|INT16|INT32|int64|uint8|UINT16|uint32|uint64|logical|char
Complex Number Support:Yes

沿着操作的维度,指定为正整数标量。如果未指定值,则默认值是第一个数组维度,其大小不等于1。

考虑二维输入阵列,A:

  • 全部(A,1)在列的连续元素上工作A和returns a row vector of logical values.

  • 全部(A,2)在行的连续元素上工作A和returns a column vector of logical values.

Data Types:double|single|int8|INT16|INT32|int64|uint8|UINT16|uint32|uint64

尺寸向量,指定为正整数的向量。每个元素代表输入阵列的维度。指定的操作尺寸中输出的长度为1,而其他功能保持不变。

Consider a 2-by-3-by-3 input array,A。这n全部(A,[1 2])返回一个1 x-1 x-3数组,其元素表示每个页面的非零值A

Data Types:double|single|int8|INT16|INT32|int64|uint8|UINT16|uint32|uint64

Output Arguments

collapse all

逻辑数组,作为标量,向量,矩阵或多维数组返回。尺寸Aacted on by全部has size1inB

Extended Capabilities

在R2006a之前引入