在 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