matlab find没找到,MATLAB find()函数 (还有一些问题不明白)

本文详细介绍了MATLAB中的find函数,包括如何找到非零元素的线性索引、第一k个非零元素的索引以及最后一k个非零元素的索引。此外,还展示了find函数在处理稀疏矩阵时的用法,以及当find函数与逻辑表达式结合时如何返回非零元素的逻辑数组。示例代码展示了find函数的各种用法,包括查找大于特定值的元素的索引等。
摘要由CSDN通过智能技术生成

find

Find indices索引 and values值 of nonzero elements非零元素

Syntax句法

ind = find(X)

ind = find(X, k)

ind = find(X, k, 'first')

ind = find(X, k, 'last')

[row,col] = find(X, ...)

[row,col,v] = find(X, ...)

Description

ind = find(X)

locates all nonzero elements非零元素 of array X,

and returns the linear indices线性下标 of those elements in vector

ind在向量ind中.

If X is a row vector行向量, then ind is a row vector行向量;

otherwise, ind is a column vector列向量.

If X contains no nonzero elements or is

an empty array, then ind is an empty array空向量.

ind = find(X, k) or ind = find(X, k, 'first')

returns at most至多,不超过 the first k indices

corresponding to与...一致 the nonzero entries个数 of X. %

也就是至多只能输出X中符合条件的前k项

k must be a positive integer正整数, but it can be of any numeric

data type数字数据类型.

ind = find(X, k, 'last') returns at

most the last k indices corresponding to the nonzero entries of

X.

[row,col] = find(X, ...) returns

the row and column indices of the nonzero entries in the matrix

X.

This syntax句法 is especially useful when working with sparse

matrices稀疏矩阵. If X is

an N-dimensional arrayN维数组 with N > 2, col contains

linear

indices for the columns.

For example,

for a 5-by-7-by-3 array X with

a nonzero element at X(4,2,3),

find returns 4 in row

and 16 in col.

That is, (7 columns in

page 1) + (7 columns in page 2) + (2 columns in page 3) =

16.

[row,col,v] = find(X, ...) returns

a column or row vector v of the nonzero entries in X,

as well as row and column indices. If X is a logical

expression_r_r逻辑表达式,

then v is a logical array逻辑数组.

Output v contains the non-zero elements of the logical array

obtained by evaluating the expression_r_r表达式 X.

For example,

A= magic(4)

A =

16 2 3 13

5 11 10 8

9 7 6 12

4 14 15 1

[r,c,v]= find(A>10);

r', c', v'

ans =

1 2 4 4 1 3

ans =

1 2 2 3 4 4

ans =

1 1 1 1 1 1

Here the returned vector v is a logical array逻辑数组 that

contains the nonzero elements非零项 of N where N=(A>10)

% N是X中>10的元素组成的向量

Examples

Example 1

X = [1 0 4 -3 0 0 0 8 6];

indices = find(X) returns linear indices线性下标 for the nonzero

entries非零项 of X.

indices =

1 3 4 8 9

Example 2

You can use a logical expression_r_r逻辑表达式 to define X.

For example,

find(X > 2) returns linear indices线性下标

corresponding to the entries项 of X that

are greater than 2.

ans =

3 8 9

Example 3

The following find command下面的find命令

X = [3 2 0; -5 0 7; 0 0 1];

[r,c,v] = find(X)

returns a vector of row行 indices of the nonzero entries个数 of

X

r =

1

2

1

2

3

a vector of column列 indices of the nonzero entries个数 of X

c =

1

1

2

3

3

and a vector containing包含 the nonzero entries非零项 of X.

v =

3

-5

2

7

1

Example 4

The expression_r_r表达式

[r,c,v] = find(X>2)

returns a vector of row indices of the nonzero entries个数 of

X(>2)

r =

1

2

a vector of column indices of the nonzero entries个数 of

X(>2)

c =

1

3

and a logical array逻辑数组 that contains the non zero elements非零元素

of N(矩阵) where N=(X>2). %

N是X中>2的元素组成的向量,且v=find(N)

v =

1

1

Recall that when you use find on a logical

expression_r_r逻辑表达式,

the output vector v does not contain the nonzero entries非零项

of the input array输入的数组. Instead, it contains the nonzero values非零值

returned after

evaluating the logical expression_r_r逻辑表达式.

Example 5

Some operations操作 on a vector

x = [11 0 33 0 55]';

find(x)

ans =

1

3

5

find(x == 0)

ans =

2

4

find(0 < x & x <

10*pi)

ans =

1

Example 6

For the matrix

M = magic(3)

M =

8 1 6

3 5 7

4 9 2

find(M > 3, 4)

returns the indices下标 of the first four entries前四项(也就是只输出前四项) of

M that are greater than 3.

ans =

1

3

5

6

Example 7

If X is a vector of all zeros, find(X) returns an empty

matrix一个空矩阵.

For example,indices = find([0;0;0])

indices =

Empty matrix空矩阵: 0-by-1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值