MATLAB中數組索引的方法教程

在 MATLAB 中,數組用于表示信息和數據。您可以使用索引來訪問數組的元素。在 MATLAB 中,數組索引從 1 開始。要查找數組中元素的索引,可以使用find()函數。使用find()函數,您可以從數組中找到索引和元素。find()函數返回一個包含數據的向量 。

句法:

  • find(X) :返回一個包含元素索引的向量

  • find(X,n) : 返回 X 中元素的前 n 個索引

  • find(X,n, Direction):根據Direction在X中找到n個索引,其中Direction - ' first '或' last '

  • [ row , col] = find():返回數組中元素的行列下標

  • [row,col,V] = find():返回包含非零元素的向量 V

現在讓我們看看如何在示例的幫助下使用find()函數查找數組中任何元素的索引。

找到(x)

find(X) 返回一個向量,其中包含 數組 X 中每個非零元素的線性索引。

示例 1:

  • MATLAB

% MATLAB code for find an index of any

% element in an array using the find()

array = [1 2 3 4 5 6]

% find() will get the index of element

% store it in the index

index = find(array==3)

輸出:

MATLAB中數組索引的方法教程的圖1

注意:如果數組包含重復項,則 find(X) 函數將返回該整數的所有索引。

示例 2:

  • MATLAB

% MATLAB code for if the array contains

% duplicate elements

array = [1 2 3 4 5 6 2 4 2]

% find() will get the index of element

% store it in the index

index = find(array==2)

輸出:

MATLAB中數組索引的方法教程的圖2

當數組包含重復值時,find()函數將打印相應元素的所有索引。因此,如果您不想要該元素的所有索引,則可以使用find(X,n)函數。

找到(X,n)

返回 X 中元素的前 n 個索引。

例子:

  • MATLAB

% MATLAB code for return first

% n indices of the elements in X

array = [1 2 3 4 5 6 2 4 2]

% find() will get the index of element

% gets the first index of 2

% store it in the index

index = find(array==2,1)

輸出:

MATLAB中數組索引的方法教程的圖3

查找(X,n,方向)

您還可以從數組中的兩個方向找到元素的索引。通過使用 find(X,n,Direction),兩個方向都意味著從開始和從最后開始。此函數根據方向在 X 中找到 n 個索引。Direction 參數接受“first”或“last”。如果方向是第一個,它將返回該相應元素的前 n 個索引,或者如果方向是最后一個,它將通過從數組的末尾遍歷來返回索引。默認情況下,Direction 參數為“first”。

示例 1:

  • MATLAB

% MATLAB code for find the index of

% the elements from both directions

% in the array

array = [1 2 3 4 5 6 2 4 2]

% find() will get the index of element

% store it in the index

index = find(array==2,2,'first')

輸出:

MATLAB中數組索引的方法教程的圖4

示例 2:

  • MATLAB

% array of integers

array = [1 2 3 4 5 6 2 4 2]

% find() will get the index of element

% store it in the index

index = find(array==2,2,'last')

輸出:

MATLAB中數組索引的方法教程的圖5

[行,列] = 查找(x)

要在 3 維數組中查找元素的索引,您可以使用語法[row,col] = find(x)這將為您提供元素所在的行和列。

例子:

  • MATLAB

% MATLAB code for Finding an index

% of an element in a 3-D array

array = [1 2 3; 4 5 6; 7 8 9]

% find() will get the index of element

% prints the row and column of the element

[row,col] = find(array==5)

輸出:

MATLAB中數組索引的方法教程的圖6

[行,列,v] = 查找(X)

如果要查找 3 維數組中存在的所有非零元素的索引,可以使用[row,col,v] = find(X)其中 X 是我們的數組。這將找到數組中存在的所有非零元素的所有索引并將它們存儲到向量v中。

例子:

  • MATLAB

% MATLAB code for find the indices of

% all the non-zero elements present in the 3-D array

x = [1 9 0; 3 -1 0; 0 0 7]

% find() will get the indices of the elements

% and the vector will store all the non-zero elements

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

輸出:

MATLAB中數組索引的方法教程的圖7

總結

現在的專業軟件都是越做越大,功能成倍成倍地增加,而處理的對象也是越來越復雜,特別是使用一些仿真軟件在高精度下建模仿真的時候,因為PC上硬件的發展速度慢于軟件功能復雜化的速度,就造成了我們的仿真看起來是越跑越慢了。

想要高效仿真但又不想更換電腦怎么辦?現在只要在贊奇超高清云工作站上進行仿真即可,普通電腦秒變高配電腦。你只需要提供一臺普通配置的舊電腦,無論你本地電腦配置多少,下載贊奇云工作站客戶端手機號注冊登陸即可選擇你需要的高配置GPU機型,任你自己選擇更換。

登錄后免費查看全文
立即登錄
App下載
技術鄰APP
工程師必備
  • 項目客服
  • 培訓客服
  • 平臺客服

TOP