📜  如何在MATLAB中查找数组中数字的位置?

📅  最后修改于: 2022-05-13 01:55:09.645000             🧑  作者: Mango

如何在MATLAB中查找数组中数字的位置?

查找数字在数组中的位置,可以使用 find()函数来完成。 find()函数用于查找指定非零元素的索引和值。

句法

find(X)

参数:该函数接受一个参数。

  • X:这是将在数组中找到其位置的指定数字。

返回值:它返回给定数字在指定数组中的位置。

示例 1

Matlab
% MATLAB code for getting the position 
% of element 4
X = [2 4 4 5 6 4]; % Initializing an array 
% of some elements
  
% Calling the find() function to find 
% the position of 4
Position_of_4 = find(X==4)


Matlab
% MATLAB code for getting the position 
% of element 4 in terms of rows and columns.
X = [2 4 4 5 6 4]; 
  
% Calling the find() function to find 
% the position of 4 in terms of rows 
% and columns
[Row, column] = find(X==4)


输出:

示例 2

MATLAB

% MATLAB code for getting the position 
% of element 4 in terms of rows and columns.
X = [2 4 4 5 6 4]; 
  
% Calling the find() function to find 
% the position of 4 in terms of rows 
% and columns
[Row, column] = find(X==4)

输出: