matlab中any 函数的作用

any

Determine whether any array elements are nonzero
Syntax

B = any(A)
B = any(A,dim)
Description

B = any(A) tests whether any of the elements along various dimensions of an array is a nonzero number or is logical 1 (true). any ignores entries that are NaN (Not a Number).

If A is a vector, any(A) returns logical 1 (true) if any of the elements of A is a nonzero number or is logical 1 (true), and returns logical 0 (false) if all the elements are zero.

If A is a matrix, any(A) treats the columns of A as vectors, returning a row vector of logical 1's and 0's.

If A is a multidimensional array, any(A) treats the values along the first nonsingleton dimension as vectors, returning a logical condition for each vector.

B = any(A,dim) tests along the dimension of A specified by scalar dim.

就是B = any(A),如果A是向量,如果向量里有非0的数,则返回1(true),如果A是矩阵,则把矩阵的列当做向量来处理,函数返回每个列向量的逻辑值;
B = any(A,dim)测试由dim表示的A的维度,返回相应逻辑值

Examples
Example 1 – Reducing a Logical Vector to a Scalar Condition

Given

A = [0.53 0.67 0.01 0.38 0.07 0.42 0.69]

then B = (A < 0.5) returns logical 1 (true) only where A is less than one half:

0 0 1 1 1 1 0

The any function reduces such a vector of logical conditions to a single condition. In this case, any(B) yields logical 1.

This makes any particularly useful in if statements:

if any(A < 0.5)do something
end

where code is executed depending on a single condition, not a vector of possibly conflicting conditions.
Example 2– Reducing a Logical Matrix to a Scalar Condition

Applying the any function twice to a matrix, as in any(any(A)), always reduces it to a scalar condition.

any(any(eye(3)))
ans =
1

Example 3 – Testing Arrays of Any Dimension

You can use the following type of statement on an array of any dimensions. This example tests a 3-D array to see if any of its elements are greater than 3:

x = rand(3,7,5) * 5;

any(x(:) > 3)
ans =
1

or less than zero:

any(x(:) < 0)
ans =
0
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值