MATLAB中函数find的使用方法概述,简单易懂+例子

您所在的位置:网站首页 find函数+1 MATLAB中函数find的使用方法概述,简单易懂+例子

MATLAB中函数find的使用方法概述,简单易懂+例子

2024-02-12 12:38| 来源: 网络整理| 查看: 265

MATLAB中函数find的使用方法总结+探索,简单易懂+例子

该函数不仅可以查找矩阵获这向量中的非零元素的位置,也可以查询满足一定逻辑条件的元素位置。

find函数总共的适用方法 index = find(X)index = find(X, k)index = find(X, k, ‘first’)%%index = find(X, k, ‘last’)%%[row, col] = find(X, …)[row, col, v] = find(X, …) 例子 index = find(X) 这种使用方式适用于向量和矩阵。其主要功能是用于寻找向量或者矩阵中非零元素的位置,并将非零元素的向量中索引or矩阵中位置返回到index中,在index中,索引存放的方式是以列向量的方式进行存错。 testMatrix = rand(3,3) testMatrix = 0.8147 0.9134 0.2785 0.9058 0.6324 0.5469 0.1270 0.0975 0.9575 index = find(testMatrix) index = 1 2 3 4 5 6 7 8 9 size_of_index = size(index) size_of_index = 9 1 index = find(X, k)index = find(X, k, ‘first’)index = find(X, k, ‘last’) 这三个具有强烈的相似之处,上述函数中,k表示的是find函数返回向量或者矩阵中非零元素的个数。2 式中,表示的是默认默认从首端返回,3 式中,指明了从’first’ (首端)返回,4 式中,指明了从’last’ (尾端)返回。 >> testMatrix = [1,0,3;2,3,0;0,4,6] testMatrix = 1 0 3 2 3 0 0 4 6 >> test_1 = find(testMatrix, 3) test_1 = 1 2 5 >> test_2 = find(testMatrix, 3, 'first') test_2 = 1 2 5 >> test_2 = find(testMatrix, 5, 'first') test_2 = 1 2 5 6 7 >> test_2 = find(testMatrix, 3, 'last') test_2 = 6 7 9 [row, col] = find(X, …)[row, col, v] = find(X, …) 两者功能基本一致,区别仅在返回值v,5和6式中,功能是将矩阵X中非零元素的行和列索引分别返回到row和col中。区别:6式中的v,表示是返回非零元素的值。 >> testMatrix = [1,0,3;2,3,0;0,4,6] testMatrix = 1 0 3 2 3 0 0 4 6 >> [row, col] = find(testMatrix) row = 1 2 2 3 1 3 col = 1 1 2 2 3 3 >> [row, col, v] = find(testMatrix) row = 1 2 2 3 1 3 col = 1 1 2 2 3 3 v = 1 2 3 4 3 6 功能探索篇:

话不多说,直接上例子

testMatrix = 1 0 3 2 3 0 0 4 6 >> [row, col, v] = find(testMatrix, 3) row = 1 2 2 col = 1 1 2 v = 1 2 3 >> [row, col, v] = find(testMatrix, 3, 'last') row = 3 1 3 col = 2 3 3 v = 4 3 6

上述情况下,寻找非零元素过程中,非零元素的值被存放在v中,也仅仅这种情况下,find函数可以返货真实值。如下例;

>> [row, col, v] = find(testMatrix>2) row = 2 3 1 3 col = 2 2 3 3 v = 1 1 1 1 >> [row, col, v] = find(testMatrix>2, 3, 'first') row = 2 3 1 col = 2 2 3 v = 1 1 1

当find函数的功能不是搜寻非零值时,v变量中存放的信息为1,仅表明矩阵中存在满足条件的值(个人理解)。

>> testMatrix = [1,0,3;2,3,0;0,4,6] testMatrix = 1 0 3 2 3 0 0 4 6 >> [row, col, v] = find(testMatrix>2&testMatrix> [row, col, v] = find(testMatrix>2&&testMatrix


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3