find

查找非零元素的索引和值

Syntax

k = find(X)

k = find(X,n)

k = find(X,n,direction)

[row,col] = find(___)

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

Description

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

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

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

  • 如果 X 不包含非零元素或为空, 则 find 返回空数组。

例子:

Zero and Nonzero Elements in Matrix

Find the nonzero elements in a 3-by-3 matrix.

X = [1 0 2; 0 1 1; 0 0 4]
X = 3×31     0     20     1     10     0     4
k = find(X)
k = 5×115789

Use the logical not operator on X to locate the zeros.

k2 = find(~X)
k2 = 4×12346

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

例子:

Elements Satisfying a Condition

Find the first five elements that are less than 10 in a 4-by-4 magic square matrix.

X = magic(4)
X = 4×416     2     3    135    11    10     89     7     6    124    14    15     1
k = find(X<10,5)
k = 5×123457

View the corresponding elements of X.

X(k)
ans = 5×159427

k = find(X,n,direction), 如果方向为 "last", 则查找与 x 中的非零元素对应的最后 n 个索引。directionis "first" 的默认值, 它查找与非零元素对应的第一个 n 索引。

例子:

Last Several Nonzero Elements

Create a 6-by-6 magic square matrix with all of the odd-indexed elements equal to zero.

X = magic(6);
X(1:2:end) = 0
X = 6×60     0     0     0     0     03    32     7    21    23    250     0     0     0     0     08    28    33    17    10    150     0     0     0     0     04    36    29    13    18    11

Locate the last four nonzeros.

k = find(X,4,'last')
k = 4×130323436

x(k)

ans =

18
    25
    15
    11


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

Elements Satisfying Multiple Conditions

Find the first three elements in a 4-by-4 matrix that are greater than 0 and less than 10. Specify two outputs to return the row and column subscripts to the elements.

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

The first instance is X(2,1), which is 8.


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

Subscripts and Values for Nonzero Elements

非零元素的下标和值

Find the nonzero elements in a 3-by-3 matrix. Specify three outputs to return the row subscripts, column subscripts, and element values.

X = [3 2 0; -5 0 7; 0 0 1]
X = 3×33     2     0-5     0     70     0     1
[row,col,v] = find(X)
row = 5×112123
col = 5×111233
v = 5×13-5271

Subscripts of Multidimensional Array

Find the nonzero elements in a 4-by-2-by-3 array. Specify two outputs, row and col, to return the row and column subscripts of the nonzero elements. When the input is a multidimensional array (N > 2), find returns col as a linear index over the N-1 trailing dimensions of X.

X = zeros(4,2,3);
X([1 12 19 21]) = 1
X =
X(:,:,1) =1     00     00     00     0X(:,:,2) =0     00     00     01     0X(:,:,3) =0     10     01     00     0
[row,col] = find(X)
row = 4×11431
col = 4×11356


最后介绍下线性索引:

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

例如, 考虑一个3乘3矩阵。您可以引用 a (22) 元素与 a (5) 和 a (23) 元素具有 a (8)。线性索引根据数组的大小而变化;a (5) 返回一个3乘3矩阵的不同位置的元素, 而不是4到4矩阵。

sub2ind 和 ind2sub 函数在下标和线性索引之间转换时非常有用。

【 MATLAB 】find 函数的使用(线性索引)相关推荐

  1. matlab 线性索引 转换,自己编写的 matlab 线性索引转换下标 函数

    matlab自带的线性索引转换下标函数必须指定下标个数,也就是数据的维度.这在实际应用中受到了限制. (什么是线性索引,什么是下标,不再介绍,相信你如果搜到了本贴,必然知道这两个概念) % 原函数: ...

  2. MATLAB中数组的原始索引和线性索引之间相互进行转换

    1 致谢 感谢MATLAB文档的帮助, 原文链接如下: https://ww2.mathworks.cn/help/matlab/math/array-indexing.html 2 前言 今天在学习 ...

  3. 【matlab矩阵运算】06、matlab索引:位置索引、线性索引和逻辑索引

    官网内容:数组索引 1 位置索引 1.1 (行号,列号)索引 要访问矩阵中的某个元素,需依序指定该元素的行号和列号. >> AA =1 2 34 5 6>> A(1,2)ans ...

  4. matlab 线性索引 转换,matlab – 如何获取numpy数组的线性索引(sub2ind)

    Matlab提供了功能 sub2ind,该函数"返回矩阵的行和列下标-的线性索引等价物". 我需要这个sub2ind函数或类似的东西,但是我没有找到任何类似的Python或Nump ...

  5. matlab狄利克雷函数,数论入门1——莫比乌斯函数,欧拉函数,狄利克雷卷积,线性筛,莫比乌斯反演,杜教筛...

    数论入门1 一个菜鸡对数论的一点点理解... 莫比乌斯函数 定义函数$\mu(n)$为: 当n有平方因子时,$\mu(n)=0$. 当n没有平方因子时,$\mu(n)=(-1)^{\omega(n)} ...

  6. MATLAB reshape()函数和sub2ind()函数

    题目描述 已知A为4*5的矩阵 12 3 4 7 8 5 6 9 11 13 2 1 15 20 21 10 6 11 8 9 完成如下操作:将A(2,4)的11和A(3, 2)的1删除后,保持数据次 ...

  7. matlab watershed函数简单实现_薛定宇教授大讲堂(卷):MATLAB程序设计|文末赠书...

    00作者简介 薛定宇 分别在沈阳工业大学.东北大学和英国Sussex大学获得学士(1985年).硕士(1988年)和博士学位(1992年),1997年任东北大学信息学院教授.深耕于计算机在数学与自动控 ...

  8. 【整理】Matlab常用函数

    第一篇:Matlab软件函数 一.软件操作函数 1)命令窗口函数: clc:清空命令窗口,使用向上箭头翻看命令. open:打开文件,文本文件(*.doc),可执行文件(*.exe),图形文件(*.f ...

  9. matlab stem函数坐标轴_MATLAB中stem函数用法

    stem(Y) 将数据序列Y从x轴到数据值按照茎状形式画出,以圆圈终止.如果Y是一个矩阵,则将其每一列按照分隔方式画出. stem(X,Y)在X的指定点处画出数据序列Y.  stem(...,'fil ...

最新文章

  1. JS实现html国际化二
  2. 创建一个带副本机制的topic
  3. 页面跳转的方法以及301 和 302的区别
  4. Tensorflow异常集锦
  5. 来自一位程序员女友的内心独白
  6. dumpsys gfxinfo packacges计算帧率
  7. 【AtCoder】ARC100 题解
  8. java blowfish ecb,node.js – 使用nodejs crypto和php的mcrypt解密blowfish-ecb
  9. docker及入门使用(centos7.6)
  10. esp32之arduino配置下载提速
  11. 3dmax 视频全集
  12. 国家企业信用信息查询工商数据爬虫
  13. c语言程序坐标反算,坐标正算反算公式讲解
  14. office精英俱乐部_开放组织读书俱乐部:收回精英制
  15. ffmpeg项目编译出错问题解决方案.
  16. SPSS回归分析结果解读【来自百度知道】
  17. redis单点故障方案
  18. python实现屏幕视频录制_Python实现屏幕录制功能的代码
  19. poco c++感性认识
  20. 【2.5万字】详解 Python-docx 自动生成word图文报告

热门文章

  1. rhel6   openldap
  2. java交易系统_基于SSM框架的JAVA二手交易系统
  3. 优化mysql服务器硬件包括_MySQL优化之一:服务器硬件和操作系统
  4. ajax从页面向action传递json 公司--》
  5. docker部署nacos单机版
  6. php 5.3 construct_PHP 5.3新增魔术方法__invoke概述
  7. getcontentpane java_java – 为什么getcontentpane()未定义?
  8. 第十六届全国大学生智能车竞赛线上比赛(广东+西南科技大学)成绩排名与获奖信息
  9. 2021年春季学期-信号与系统-第十三次作业参考答案-第九小题
  10. 2021年春季学期-信号与系统-第十三次作业参考答案-第三小题