如何在 MATLAB 中查詢陣列中元素的索引?
在 MATLAB 中,陣列是一種用於儲存數值資料和資訊的資料結構。儲存在陣列中的值稱為其元素。元素在陣列中的位置稱為元素的索引。元素的索引用於訪問元素或提供元素的引用。
在 MATLAB 中,使用 1 基索引,這意味著陣列第一個元素的索引為 1。
考慮以下示例陣列:
A = [20 35 74 92 60];
在此陣列中,元素“20”的索引為 1,“35”的索引為 2,依此類推。
在陣列中,元素的索引在各種操作和計算中至關重要。
本教程主要用於學習如何在陣列中查詢特定元素的索引。為此,MATLAB 提供了幾個我們可以使用的內建函式和方法。讓我們討論使用 MATLAB 查詢陣列中元素索引的不同方法。
(1). 使用“find”函式查詢陣列中元素的索引
在 MATLAB 中,有一個內建函式“find”可用於確定特定元素的索引。
語法
I = find(array);
此語法將返回一個向量,其中包含陣列中所有非零元素的索引。
為了理解此函式,讓我們在 MATLAB 中舉個例子。
示例
% MATLAB code to find index of a specific element in an array
% Create a sample array
A = [1 5 7 3 4 6 8 2 6 0 9 5];
% Find the indices of elements
I = find(A);
% Display the input array and array of indices
disp('The input array is:');
disp(A);
disp('The array of indices is:');
disp(I);
輸出
The input array is:
1 5 7 3 4 6 8 2 6 0 9 5
The array of indices is:
1 2 3 4 5 6 7 8 9 11 12
(2). 使用“find”函式查詢陣列中前“n”個元素的索引
“find”也可用於查詢陣列中前“n”個元素的索引。
語法
I = find(array, n);
此語法將返回一個向量,其中包含陣列中所有前“n”個非零元素的索引。
示例
為了理解此函式,讓我們在 MATLAB 中舉個例子。
% MATLAB code to find index of first "n" element in an array
% Create a sample array
A = [1 5 7 3 4 6 8 2 6 0 9 5];
% Find the indices of elements
I = find(A, 4);
% Display the input array and array of indices
disp('The input array is:');
disp(A);
disp('The indices of elements are:');
disp(I);
輸出
The input array is:
1 5 7 3 4 6 8 2 6 0 9 5
The indices of elements are:
1 2 3 4
(3). 使用“find”函式在指定方向上查詢陣列中“n”個元素的索引
“find”也可用於在指定方向上查詢陣列中“n”個元素的索引。為此,使用以下語法。
語法
I = find(array, n, dir);
這裡,變數“dir”可以設定為“first”或“last”。當“dir = first”時,“find”函式將返回一個包含前“n”個元素索引的向量,而當“dir = last”時,“find”函式將返回一個包含陣列最後“n”個元素索引的向量。
示例
為了理解此語法,讓我們在 MATLAB 中舉個例子。
% MATLAB code to find index of "n" elements in an array in a specified direction
% Create a sample array
A = [1 5 7 3 4 6 8 2 6 0 9 5];
% Find the indices of first 3 elements
I_first = find(A, 3, 'first');
% Find the indices of last 3 elements
I_last = find(A, 3, 'last');
% Display the input array and indices vectors
disp('The input array is:');
disp(A);
disp('The indices of first 3 elements are:');
disp(I_first);
disp('The indices of last 3 elements are:');
disp(I_last);
輸出
The input array is:
1 5 7 3 4 6 8 2 6 0 9 5
The indices of first 3 elements are:
1 2 3
The indices of last 3 elements are:
9 11 12
(4). 使用“find”函式查詢陣列中指定元素的索引
在 MATLAB 中,我們可以查詢滿足特定條件的元素的索引。為此,我們可以使用“find”函式的以下語法。
語法
I = find(array == element);
此語法將返回陣列中指定元素的索引。
示例
為了理解此函式,讓我們在 MATLAB 中舉個例子。
% MATLAB code to find index of an element meeting a condition
% Create a sample array
A = [1 5 7 3 4 6 8 2 6 0 9 5];
% Specify the element whose index to be found
e = 6;
% Find the index of element
I = find(A == e);
% Display the input array and index of element
disp('The input array is:');
disp(A);
disp('The index of specified element is:');
disp(I);
輸出
The input array is:
1 5 7 3 4 6 8 2 6 0 9 5
The index of specified element is:
6 9
(5). 使用“ismember”函式查詢元素的索引
在 MATLAB 中,“ismember”函式也可用於查詢陣列中元素的索引。為此,使用以下語法。
語法
[x, I] = ismember(element, array);
這裡,“x”是一個變數,如果在陣列中找到指定元素,則其值為“1”,變數“I”將儲存元素的索引。
示例
讓我們藉助示例來了解此函式的用法。
% MATLAB code to find index of an element using "ismember" function
% Create a sample array
A = [1 5 7 3 4 6 8 2 6 0 9 5];
% Specify the element whose index to be found
e = 6;
% Find the index of the element
[x, I] = ismember(e, A);
% Show whether the element is found or not
if x == 1
disp('The specified element is found in the array.');
else
disp('The specified element is not found in the array.');
end
% Display the input array and index of element
disp('The input array is:');
disp(A);
disp('The index of specified element is:');
disp(I);
輸出
The specified element is found in the array.
The input array is:
1 5 7 3 4 6 8 2 6 0 9 5
The index of specified element is:
9
結論
總之,陣列中元素的索引是元素在陣列中的位置。在本教程中,我透過示例解釋了查詢陣列中元素索引的不同方法。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP