如何在 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