在 MATLAB 中提取矩阵中连续条目的子集
MATLAB 是一款强大的矩阵操作工具。本文将探讨如何使用 MATLAB 提取矩阵中连续条目的子集。但在开始之前,让我们先概述一下矩阵中连续条目的子集。
在矩阵中,位于行或列或两者按顺序排列的一组元素称为矩阵中连续条目的子集。可以通过指定列和行的范围来提取矩阵中**连续条目的子集**。矩阵中连续条目的子集用于分析和操作大型矩阵的特定部分。
现在,让我们讨论如何使用 MATLAB 提取矩阵中连续条目的子集。
在 MATLAB 中,提取矩阵中连续条目的子集可以有以下三种情况
从矩阵中的连续行提取子集
从矩阵中的连续列提取子集
从矩阵中的连续行和列一起提取子集
让我们借助示例详细讨论每种情况。
从矩阵中的连续行提取子集
在 MATLAB 中,我们可以使用数组索引从矩阵中的连续行提取子集。
示例
让我们借助示例程序来理解这个概念。
% MATLAB program to extract subsets from consecutive rows
% Create a sample matrix
M = [1, 2, 3, 4, 5; 6, 7, 8, 9, 10; 11, 12, 13, 14, 15; 16, 17, 18, 19, 20];
% Specify the range of consecutive rows to extract
S = 2; % Start row
E = 4; % End row
% Extract the subsets of consecutive rows
sub = M(S:E, :); % Array indexing
% Display the original matrix and the extracted subset
disp('Original matrix is:');
disp(M);
disp('Extracted subsets of consecutive rows is:');
disp(sub);
输出
Original matrix is:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
Extracted subsets of consecutive rows is:
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
代码解释
在这个示例中,我们首先创建一个矩阵“M”。然后,我们指定要提取的连续行的数量,“S”是起始行,“E”是结束行。之后,我们使用数组索引从矩阵中提取指定的连续行。
最后,我们使用“disp”函数显示原始矩阵和提取的矩阵。
从矩阵中的连续列提取子集
与连续行类似,我们也可以使用数组索引从矩阵中的连续列提取子集。
示例
以下 MATLAB 程序演示了如何使用 MATLAB 从连续列提取子集。
% MATLAB program to extract subsets from consecutive columns
% Create a sample matrix
M = [1, 2, 3, 4, 5; 6, 7, 8, 9, 10; 11, 12, 13, 14, 15; 16, 17, 18, 19, 20];
% Specify the range of consecutive columns to extract
S = 2; % Start column
E = 4; % End column
% Extract the subsets of consecutive columns
sub = M(:, S:E); % Array indexing
% Display the original matrix and the extracted subset
disp('Original matrix is:');
disp(M);
disp('Extracted subsets of consecutive columns is:');
disp(sub);
输出
Original matrix is:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
Extracted subsets of consecutive columns is:
2 3 4
7 8 9
12 13 14
17 18 19
代码解释
此 MATLAB 程序的实现和执行与上述程序相同。此 MATLAB 代码使用数组索引提取连续列的子集并将结果存储在变量“sub”中。
从矩阵中的连续行和列提取子集
在 MATLAB 中,要提取矩阵中连续行和列的子集,我们同时使用行和列索引。
示例
以下 MATLAB 程序演示了如何从矩阵中提取连续行和列的矩形子集。
% MATLAB program to extract subsets from consecutive rows and columns
% Create a sample matrix
M = [1, 2, 3, 4, 5; 6, 7, 8, 9, 10; 11, 12, 13, 14, 15; 16, 17, 18, 19, 20];
% Specify the range of consecutive rows to extract
S_R = 1; % Start row
E_R = 3; % End row
% Specify the range of consecutive columns to extract
S_C = 2; % Start column
E_C = 4; % End column
% Extract the subsets of consecutive rows and columns using array indexing
sub = M(S_R:E_R, S_C:E_C);
% Display the original matrix and the extracted subset
disp('Original matrix is:');
disp(M);
disp('Extracted subsets of consecutive rows and columns is:');
disp(sub);
输出
Original matrix is:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
Extracted subsets of consecutive rows and columns is:
2 3 4
7 8 9
12 13 14
代码解释
在此 MATLAB 代码中,我们同时对行和列使用了数组索引,以从矩阵中提取连续行和列的子集。此代码的结果为一个矩形子集。
结论
在本教程中,我们借助示例程序演示了如何从矩阵中提取连续的行或列或两者。在 MATLAB 中,可以使用数组索引的概念轻松地执行此功能。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP