如何在MATLAB中選擇矩陣的隨機行?


在MATLAB中,我們可以對矩陣執行各種操作。其中一項操作是從矩陣中選擇隨機行。MATLAB提供了幾種不同的方法來從矩陣中選擇隨機行。在本教程中,我將解釋使用MATLAB從矩陣中隨機選擇行的不同方法。

在MATALB中選擇隨機行

在MATLAB中,我們有各種內建函式可用於隨機選擇矩陣的行。

以下是從矩陣中隨機選擇行的一些常用函式:

  • randperm() 函式

  • randsample() 函式

  • randi() 函式

  • datasample() 函式

讓我們討論一下在MATLAB中使用這些函式從矩陣中隨機選擇行的用法。

MATLAB中的randperm()函式

“randperm()”函式是MATLAB庫中的內建函式。此函式生成輸入數字的隨機排列。

我們可以使用此函式生成矩陣行索引的隨機排列。然後,可以使用此隨機生成的排列從矩陣中選擇隨機行。

下面介紹了使用MATLAB中的“randperm”函式從矩陣中選擇隨機行的步驟。

  • 步驟1 - 建立一個矩陣。

  • 步驟2 - 指定要隨機選擇的行數。

  • 步驟3 - 確定矩陣的行數。

  • 步驟4 - 使用“randperm”函式生成矩陣行索引的隨機排列。

  • 步驟5 - 選擇與步驟4中生成的隨機行索引對應的行。

  • 步驟6 - 顯示結果。

示例1

讓我們透過MATLAB中的一個示例來了解這些步驟。

% MATLAB code to select random rows from matrix using randperm function
% Create a sample matrix
mat = [10 20 40 60; 12 14 10 74; 40 50 80 90; 30 12 35 24; 86 24 74 36];

% Specify the number of rows to select
num_rows = 2;	% Adjust as required

% Determine the number of rows in the matrix
total_rows = size(mat, 1);

% Generate random permutation of row indices
p = randperm(total_rows, num_rows);

% Select rows according to random row indices
random_rows = mat(p, :);

% Display the input matrix and selected random rows
disp('Input Matrix:');
disp(mat);

disp('Randomly Selected Rows:');
disp(random_rows);

輸出

以下是輸出:

Input Matrix:
    10    20    40    60
    12    14    10    74
    40    50    80    90
    30    12    35    24
    86    24    74    36
Randomly Selected Rows:
    40    50    80    90
    30    12    35    24

這就是如何在MATLAB中使用“randperm”函式從矩陣中選擇隨機行的方法。執行此程式碼時,它將返回從矩陣中隨機選擇的不同行。

MATLAB中的randsample()函式

在MATLAB中,可以使用“randsample()”函式從矩陣中選擇隨機行。此函式用於獲取隨機選擇的行的索引。然後,使用這些索引從矩陣中選擇隨機行。

下面解釋了使用“randsample”函式從矩陣中選擇隨機行的分步過程。

  • 步驟1 - 建立一個矩陣。

  • 步驟2 - 指定要選擇的隨機行數。

  • 步驟3 - 使用“randsample”函式獲取隨機選擇的行的索引。

  • 步驟4 - 使用隨機索引從矩陣中選擇隨機行。

  • 步驟5 - 顯示結果。

示例2

讓我們來看一個MATLAB程式設計示例來實現這些步驟。

% MATLAB code to select random rows from matrix using randsample function
% Create a sample matrix
mat = [10 20 40 60; 12 14 10 74; 40 50 80 90; 30 12 35 24; 86 24 74 36];

% Specify the number of rows to select
num_rows = 2;	% Adjust as required

% Determine the number of rows in the matrix
total_rows = size(mat, 1);

% Get the indices of randomly selected rows
p = randsample(total_rows, num_rows);

% Select rows according to random row indices
random_rows = mat(p, :);

% Display the input matrix and selected random rows
disp('Input Matrix:');
disp(mat);

disp('Randomly Selected Rows:');
disp(random_rows);

輸出

執行此程式碼時,將產生以下輸出:

Input Matrix:
    10    20    40    60
    12    14    10    74
    40    50    80    90
    30    12    35    24
    86    24    74    36
Randomly Selected Rows:
    40    50    80    90
    86    24    74    36

MATLAB中的randi()函式

在MATLAB中,我們還可以使用“randi”函式從矩陣中隨機選擇行。

使用“randi”函式從矩陣中選擇隨機行的步驟與上述兩種方法相同。

示例3

讓我們來看一個示例,以瞭解使用“randi”函式從矩陣中選擇隨機行的用法。

% MATLAB code to select random rows from matrix using randi function
% Create a sample matrix
mat = [10 20 40 60; 12 14 10 74; 40 50 80 90; 30 12 35 24; 86 24 74 36];

% Specify the number of rows to select
num_rows = 2;	% Adjust as required

% Determine the number of rows in the matrix
total_rows = size(mat, 1);

% Generate indices of random rows
p = randi(total_rows, 1, num_rows);

% Select rows according to random row indices
random_rows = mat(p, :);

% Display the input matrix and selected random rows
disp('Input Matrix:');
disp(mat);

disp('Randomly Selected Rows:');
disp(random_rows);

輸出

以下是輸出:

Input Matrix:
    10    20    40    60
    12    14    10    74
    40    50    80    90
    30    12    35    24
    86    24    74    36
Randomly Selected Rows:
    10    20    40    60
    12    14    10    74

MATLAB中的datasample()函式

在MATLAB中,還有一個名為“datasample”的函式,可用於從矩陣中隨機選擇行。

以下是使用“datasample”函式從矩陣中選擇隨機行的步驟。

  • 步驟1 - 建立一個矩陣。

  • 步驟2 - 指定要從矩陣中隨機選擇的行數。

  • 步驟3 - 使用“datasample”函式從矩陣中隨機選擇行。

  • 步驟4 - 顯示隨機選擇的行。

示例4

讓我們來看一個例子,瞭解在MATLAB中實現這些步驟。

% MATLAB code to select random rows from matrix using datasample function
% Create a sample matrix
mat = [10 20 40 60; 12 14 10 74; 40 50 80 90; 30 12 35 24; 86 24 74 36];

% Specify the number of rows to select
num_rows = 3;	% Adjust as required

% Select the rows randomly from the matrix
random_rows = datasample(mat, num_rows);

% Display the input matrix and selected random rows
disp('Input Matrix:');
disp(mat);

disp('Randomly Selected Rows:');
disp(random_rows);

輸出

以下是輸出:

Input Matrix:
    10    20    40    60
    12    14    10    74
    40    50    80    90
    30    12    35    24
    86    24    74    36
Randomly Selected Rows:
    40    50    80    90
    86    24    74    36
    10    20    40    60

結論

總而言之,MATLAB提供了各種內建函式,我們可以用它們從矩陣中隨機選擇行。在本文中,我解釋了在MATLAB中選擇矩陣隨機行的四個最常用的函式。您可以嘗試使用不同的矩陣和不同的隨機行數來執行這些程式碼。

更新於:2023年10月25日

434 次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.