📜  如何遍历MATLAB中N维矩阵中的每个元素?

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

如何遍历MATLAB中N维矩阵中的每个元素?

在 MATLAB 中,矩阵被视为二维数字数组。其他编程语言处理数字,但在 MATLAB 中,每个数字都是一个矩阵或数组。要在 MATLAB 中创建矩阵,需要通过添加逗号或空格在每行中输入数字,并用分号标记每行的结尾。

假设,以下代码创建了一个 2×2 矩阵:

例子:

Matlab
% MATLAB Code for 2x2 matrix 
M = [1 2 ; 3 4]


Matlab
% MATLAB Code for iteration
% Create a matrix of 3-by-4 dimension
M = [2 3 4 5;
    6 7 8 9 ;
    0 1 6 8];
  
% Get rows and columns of matrix using
% size() function
[rows,column]=size(M);
  
% Iterate through each row and display 
% each column element of that row
for i=1:rows
for j=1:column
x=M(i,j);
fprintf( '   %d    ',x);
end
     
% Add a line break after one row is displayed
fprintf('\n');
end


Matlab
% MATLAB Code for iteration using semicolon
% Create a matrix of 3-by-4 dimension
M=[2 3 4 5; 6 7 8 9 ; 0 1 6 8];
  
%Most often in matrix we use vectors subscripts 
% to access elements in specific range. For example
% to access rows 2nd and 3rd, 1st and 2nd column use indexing as:
M(2:3,1:2)
  
%2:3 specifies that extract elements of 2nd and 3rd row 
%1:2 for column index tells to extract 1st and 2nd column elements.


Matlab
% MATLAB Code for the whole matrix iteration
% by using range based indexing 
% Create a matrix of 3-by-4 dimension
M=[2 3 4 5; 6 7 8 9 ; 0 1 6 8];
  
%similarly if you want to iterate through whole matrix, just specify its
%rows and columns
M(1:3,1:4)%iterate through rows 1-3 and column 1-4.


Matlab
% MATLAB Code for whole matrix can be iterated 
% without explicitly defining range vectors
%Create a matrix of 3-by-4 dimension
M=[2 3 4 5; 6 7 8 9 ; 0 1 6 8];
  
%Leave rows and column place as empty and 
% MATLAB takes it as 'all' 
M(:,:)
  
%whole matrix iterated.


Matlab
% MATLAB Code for iteration using numel() 
% Create a matrix of 3-by-4 dimension
M=[2 3 4 5; 6 7 8 9 ; 0 1 6 8];
  
% create ouptput vector for storing outputs
output=[];
  
% get number of elements in matrix M
n=numel(M);
  
% loop through all elements and store values in output array.
for i=1:n
output(i)=M(i);
end
  
% prints output array.
output


输出:



方法一

在此方法中,当您需要跟踪当前所在的索引时,我们会迭代一个矩阵。

例子:

MATLAB

% MATLAB Code for iteration
% Create a matrix of 3-by-4 dimension
M = [2 3 4 5;
    6 7 8 9 ;
    0 1 6 8];
  
% Get rows and columns of matrix using
% size() function
[rows,column]=size(M);
  
% Iterate through each row and display 
% each column element of that row
for i=1:rows
for j=1:column
x=M(i,j);
fprintf( '   %d    ',x);
end
     
% Add a line break after one row is displayed
fprintf('\n');
end

输出:

输出矩阵

方法二

当您需要跟踪当前所在的索引时,可以使用上述迭代矩阵的方法。还有另一种使用分号迭代矩阵的方法。在矩阵中,如上所述,可以通过指定其在矩阵中的位置来访问每个元素。如果要遍历某些特定元素,请使用基于范围的索引,如下所示:

示例 1:

MATLAB



% MATLAB Code for iteration using semicolon
% Create a matrix of 3-by-4 dimension
M=[2 3 4 5; 6 7 8 9 ; 0 1 6 8];
  
%Most often in matrix we use vectors subscripts 
% to access elements in specific range. For example
% to access rows 2nd and 3rd, 1st and 2nd column use indexing as:
M(2:3,1:2)
  
%2:3 specifies that extract elements of 2nd and 3rd row 
%1:2 for column index tells to extract 1st and 2nd column elements.

输出:

输出

类似地,可以使用基于范围的索引来迭代整个矩阵:

示例 2:

MATLAB

% MATLAB Code for the whole matrix iteration
% by using range based indexing 
% Create a matrix of 3-by-4 dimension
M=[2 3 4 5; 6 7 8 9 ; 0 1 6 8];
  
%similarly if you want to iterate through whole matrix, just specify its
%rows and columns
M(1:3,1:4)%iterate through rows 1-3 and column 1-4.

输出:

输出

扩展上述思想,整个矩阵可以在不明确定义范围向量的情况下进行迭代,如下所示:

示例 3:

MATLAB

% MATLAB Code for whole matrix can be iterated 
% without explicitly defining range vectors
%Create a matrix of 3-by-4 dimension
M=[2 3 4 5; 6 7 8 9 ; 0 1 6 8];
  
%Leave rows and column place as empty and 
% MATLAB takes it as 'all' 
M(:,:)
  
%whole matrix iterated.

输出:

输出

方法三

在 MATLAB 中有一个函数numel可以给出矩阵中元素的数量。使用它遍历矩阵并显示矩阵的每个元素,如下所示:

例子:

MATLAB

% MATLAB Code for iteration using numel() 
% Create a matrix of 3-by-4 dimension
M=[2 3 4 5; 6 7 8 9 ; 0 1 6 8];
  
% create ouptput vector for storing outputs
output=[];
  
% get number of elements in matrix M
n=numel(M);
  
% loop through all elements and store values in output array.
for i=1:n
output(i)=M(i);
end
  
% prints output array.
output

输出:

输出

这里的输出与上面不同,因为矩阵的元素是按列访问的。当循环从 1 到矩阵中的多个元素时,每个元素都根据其索引进行访问。下表显示了 MATLAB 矩阵中每个元素的线性索引。

14710
25811
36912