查找非零元素的索引和值 - MATLAB find (2024)

查找非零元素的索引和值

全页折叠

语法

k = find(X)

k = find(X,n)

k = find(X,n,direction)

[row,col] = find(___)

[row,col,v] = find(___)

说明

示例

k = find(X) 返回一个包含数组 X 中每个非零元素的线性索引的向量。

  • 如果 X 为向量,则 find 返回方向与 X 相同的向量。

  • 如果 X 为多维数组,则 find 返回由结果的线性索引组成的列向量。

示例

k = find(X,n) 返回与 X 中的非零元素对应的前 n 个索引。

示例

k = find(X,n,direction)(其中 direction'last')查找与 X 中的非零元素对应的最后 n 个索引。direction 的默认值为 'first',即查找与非零元素对应的前 n 个索引。

示例

[row,col] = find(___) 使用上述语法中的任何输入参量返回数组 X 中每个非零元素的行和列下标。

示例

[row,col,v] = find(___) 还返回包含 X 的非零元素的向量 v

示例

全部折叠

矩阵中的零和非零元素

打开实时脚本

在 3×3 矩阵中查找非零元素。

X = [1 0 2; 0 1 1; 0 0 4]
X = 3×3 1 0 2 0 1 1 0 0 4
k = find(X)
k = 5×1 1 5 7 8 9

X 使用逻辑 not 运算符以查找零值。

k2 = find(~X)
k2 = 4×1 2 3 4 6

满足一个条件的元素

打开实时脚本

在 4×4 幻方矩阵中查找前五个小于 10 的元素。

X = magic(4)
X = 4×4 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
k = 5×1 2 3 4 5 7

查看 X 的对应元素。

X(k)
ans = 5×1 5 9 4 2 7

等于特定值的元素

打开实时脚本

要查找特定的整数值,请使用 == 运算符。例如,在 1×10 的奇数向量中查找等于 13 的元素。

x = 1:2:20
x = 1×10 1 3 5 7 9 11 13 15 17 19
k = find(x==13)
k = 7

要查找非整数值,请基于您的数据使用容差值。否则,由于浮点舍入误差有时会生成空矩阵。

y = 0:0.1:1
y = 1×11 0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000
k = find(y==0.3)
k = 1x0 empty double row vector
k = find(abs(y-0.3) < 0.001)
k = 4

最后几个非零元素

打开实时脚本

创建一个由等于零的所有奇数索引元素组成的 6×6 幻方矩阵。

X = magic(6);X(1:2:end) = 0
X = 6×6 0 0 0 0 0 0 3 32 7 21 23 25 0 0 0 0 0 0 8 28 33 17 10 15 0 0 0 0 0 0 4 36 29 13 18 11

查找四个非零值。

k = find(X,4,'last')
k = 4×1 30 32 34 36

满足多个条件的元素

打开实时脚本

在 4×4 矩阵中查找前 3 个大于 0 且小于 10 的元素。指定两个输出以便将行和列下标返回到这些元素。

X = [18 3 1 11; 8 10 11 3; 9 14 6 1; 4 3 15 21]
X = 4×4 18 3 1 11 8 10 11 3 9 14 6 1 4 3 15 21
[row,col] = find(X>0 & X<10,3)
row = 3×1 2 3 4
col = 3×1 1 1 1

第一个实例是 X(2,1),即 8

非零元素的下标和值

打开实时脚本

在 3×3 矩阵中查找非零元素。指定三个输出以返回行下标、列下标和元素值。

X = [3 2 0; -5 0 7; 0 0 1]
X = 3×3 3 2 0 -5 0 7 0 0 1
[row,col,v] = find(X)
row = 5×1 1 2 1 2 3
col = 5×1 1 1 2 3 3
v = 5×1 3 -5 2 7 1

多维数组的下标

打开实时脚本

在 4×2×3 数组中查找非零元素。指定两个输出 rowcol 以返回非零元素的行和列下标。如果输入是多维数组 (N > 2),find 返回 col 作为 XN-1 个尾部维度的线性索引。

X = zeros(4,2,3);X([1 12 19 21]) = 1
X = X(:,:,1) = 1 0 0 0 0 0 0 0X(:,:,2) = 0 0 0 0 0 0 1 0X(:,:,3) = 0 1 0 0 1 0 0 0
[row,col] = find(X)
row = 4×1 1 4 3 1
col = 4×1 1 3 5 6

输入参数

全部折叠

X输入数组
标量 | 向量 | 矩阵 | 多维数组

输入数组,指定为标量、向量、矩阵或多维数组。

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char
复数支持:

n要查找的非零元素数量
正整数标量

要查找的非零元素数量,指定为正整数标量。默认情况下,find(X,n) 查找 X 中的前 n 个非零元素。

direction搜索方向
'first' (默认) | 'last'

搜索方向,指定为字符串 'first''last'。使用 find(X,n,'last')X 中查找 n 个非零元素。

输出参量

全部折叠

k — 指向非零元素的索引
向量

指向非零元素的索引,以向量的形式返回。

  • 如果 X 是行向量,则 k 也是行向量。否则,k 为列向量。

  • X 是空数组或没有非零元素时,k 是空行向量或空列向量。

  • find 使用如下约定:当 X 为空矩阵 [] 时,k 也为空矩阵 []

可以使用 X(k) 返回 X 中的非零值。

row — 行下标
向量

行下标,以向量的形式返回。rowcol 一起指定与 X 中的非零元素对应的 X(row,col) 下标。

col — 列下标
向量

列下标,以向量的形式返回。rowcol 一起指定与 X 中的非零元素对应的 X(row,col) 下标。

如果 X 是多维数组且 N > 2,则 colXN-1 尾部维度的线性索引。这会保留关系 X(row(i),col(i)) == v(i)

vX 的非零元素
向量

X 的非零元素,以向量的形式返回。

详细信息

全部折叠

线性索引

线性索引允许使用单个下标创建指向数组的索引,例如A(k)。MATLAB® 将该数组视为单列向量,其中每一列附加到前一列的底部。因此,线性索引从上到下、从左到右对列中的元素编号。

例如,假设一个 3×3 矩阵。您可以用 A(5) 来引用 A(2,2) 元素,用 A(8) 来引用 A(2,3) 元素。线性索引根据数组大小而改变;A(5) 会为 3×3 矩阵和 4×4 矩阵返回不同位置上的元素。

sub2indind2sub 函数对于在下标和线性索引之间进行转换非常有用。

提示

  • 要查找符合条件的数组元素,请结合使用 find 和关系表达式。例如,find(X<5) 返回 X 中小于 5 的元素的线性索引。

  • 要直接查找 X 中满足条件 X<5 的元素,请使用 X(X<5)。尽量避免使用 X(find(X<5)) 之类的函数调用,因为这种调用中对逻辑矩阵使用的 find 完全没有必要。

  • findX>1 之类的关系运算结合在一起执行时,必须记住关系运算的结果是由 1 和 0 组成的逻辑矩阵。例如,命令 [row,col,v] = find(X>1) 会返回由 v 的逻辑值 1 (true) 组成的列向量。

  • 行下标和列下标,即 rowcolk×k = sub2ind(size(X),row,col) 中的线性索引相关。

扩展功能

版本历史记录

在 R2006a 之前推出

另请参阅

ind2sub | nonzeros | strfind | sub2ind | Short-Circuit AND | Short-Circuit OR | ismember

主题

  • 查找符合条件的数组元素
  • 数组索引
  • 关系运算
  • 稀疏矩阵

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

查找非零元素的索引和值 - MATLAB find (1)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

查找非零元素的索引和值 - MATLAB find (2024)
Top Articles
Clasificados Utah Idaho Wyoming
100 Best Rizz Pick-up Lines to Spice up Your Flirting Game | Inspirationfeed
Funny Roblox Id Codes 2023
Golden Abyss - Chapter 5 - Lunar_Angel
Www.paystubportal.com/7-11 Login
Joi Databas
DPhil Research - List of thesis titles
Shs Games 1V1 Lol
Evil Dead Rise Showtimes Near Massena Movieplex
Steamy Afternoon With Handsome Fernando
Which aspects are important in sales |#1 Prospection
Detroit Lions 50 50
18443168434
Newgate Honda
Zürich Stadion Letzigrund detailed interactive seating plan with seat & row numbers | Sitzplan Saalplan with Sitzplatz & Reihen Nummerierung
Grace Caroline Deepfake
978-0137606801
Nwi Arrests Lake County
Justified Official Series Trailer
London Ups Store
Committees Of Correspondence | Encyclopedia.com
Pizza Hut In Dinuba
Jinx Chapter 24: Release Date, Spoilers & Where To Read - OtakuKart
How Much You Should Be Tipping For Beauty Services - American Beauty Institute
Free Online Games on CrazyGames | Play Now!
Sizewise Stat Login
VERHUURD: Barentszstraat 12 in 'S-Gravenhage 2518 XG: Woonhuis.
Jet Ski Rental Conneaut Lake Pa
Unforeseen Drama: The Tower of Terror’s Mysterious Closure at Walt Disney World
Ups Print Store Near Me
C&T Wok Menu - Morrisville, NC Restaurant
How Taraswrld Leaks Exposed the Dark Side of TikTok Fame
University Of Michigan Paging System
Dashboard Unt
Access a Shared Resource | Computing for Arts + Sciences
Speechwire Login
Healthy Kaiserpermanente Org Sign On
Restored Republic
3473372961
Craigslist Gigs Norfolk
Moxfield Deck Builder
Senior Houses For Sale Near Me
Whitehall Preparatory And Fitness Academy Calendar
Trivago Myrtle Beach Hotels
Anya Banerjee Feet
Three V Plymouth
Thotsbook Com
Funkin' on the Heights
Vci Classified Paducah
Www Pig11 Net
Ty Glass Sentenced
Latest Posts
Article information

Author: Ouida Strosin DO

Last Updated:

Views: 6137

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Ouida Strosin DO

Birthday: 1995-04-27

Address: Suite 927 930 Kilback Radial, Candidaville, TN 87795

Phone: +8561498978366

Job: Legacy Manufacturing Specialist

Hobby: Singing, Mountain biking, Water sports, Water sports, Taxidermy, Polo, Pet

Introduction: My name is Ouida Strosin DO, I am a precious, combative, spotless, modern, spotless, beautiful, precious person who loves writing and wants to share my knowledge and understanding with you.