在 MATLAB 中建立零陣列


在 MATLAB 中,變數通常以矩陣或陣列的形式儲存。有時,我們需要一個**零矩陣或零陣列**來執行特定的操作。因此,MATLAB 允許我們建立零矩陣或零陣列。對於此任務,我們可以使用 MATLAB 的內建函式或手動方法。

在本文中,我們將學習使用 MATLAB 程式設計中的示例程式建立零矩陣或零陣列的不同方法。

使用 MATLAB 中的 `zeros()` 函式建立零陣列

MATLAB 提供了一個名為 `zeros()` 的內建函式,用於建立具有特定行數和列數的零矩陣或零陣列。

建立具有指定行數和列數的零陣列

我們可以使用 `zeros()` 函式建立 n × m 階的零陣列,使用以下語法。

語法

A = zeros(n, m);

這裡,`A` 是一個定義的變數,用於儲存零陣列,`n` 是表示陣列中行數的數字,`m` 是表示陣列中列數的數字。

示例

% MATLAB program to demonstrate the creation of an n x m array of zeros using `zeros()` function
% Creating a 3 x 4 array of zeros
A = zeros (3, 4);
% Display the array of zeros
disp('The array of zeros: ');
disp(A);

輸出

The array of zeros: 
     0     0     0     0
     0     0     0     0
     0     0     0     0

建立具有相同行數和列數的零陣列

以下 `zeros()` 函式的語法用於建立 n × n 階的零陣列。

語法

A = zeros(n);

其中,`n` 是陣列的階數。

示例

% MATLAB program to demonstrate the creation of an n x n array of zeros
% Creating a 3 x 3 array of zeros
A = zeros (3);
% Display the array of zeros
disp('The array of zeros: ');
disp(A);

輸出

The array of zeros: 
     0     0     0
     0     0     0
     0     0     0

建立標量零

我們只需在不帶引數的情況下呼叫 `zeros` 函式即可建立一個標量零。以下 MATLAB 程式演示了建立標量零的實現。

示例

% MATLAB program to demonstrate the creation of a scalar zero
% Creating a scalar zero
A = zeros;
% Display the scalar zero
disp('The scalar zero is: ');
disp(A);

輸出

The scalar zero is: 
     0

建立 3 維零陣列

我們也可以使用三個引數的 `zeros()` 函式來建立 3 維零陣列。為此,zeros() 函式將採用以下語法。

語法

A = zeros (a, b, c);

此函式將建立一個 a x b x c 的零陣列。

示例

% MATLAB program to demonstrate the creation of a 3-d array of zeros
% Creating a 3 x 4 x 5 array of zeros
A = zeros (3, 4, 5);
% Display the array of zeros
disp('The array of zeros: ');
disp(A);

輸出

The array of zeros: 

(:,:,1) =

     0     0     0     0
     0     0     0     0
     0     0     0     0

(:,:,2) =

     0     0     0     0
     0     0     0     0
     0     0     0     0

(:,:,3) =

     0     0     0     0
     0     0     0     0
     0     0     0     0

(:,:,4) =

     0     0     0     0
     0     0     0     0
     0     0     0     0

(:,:,5) =

     0     0     0     0
     0     0     0     0
     0     0     0     0

在 MATLAB 中手動建立零陣列

我們可以在 MATLAB 程式設計中指定程式碼來建立不同型別的零陣列。以下部分說明了手動建立零陣列。

建立大小為 1 x n 或 n 個零的零陣列

我們可以在 MATLAB 程式設計中建立大小為 1 x n 的行向量或 n 個零的陣列。

以下 MATLAB 程式演示了建立 6 (n = 6) 個零的陣列。

示例

% MATLAB program to demonstrate the creation of an array of n zeros
% Creating an array of 6 (n = 6) zeros
A = [0 0 0 0 0 0];
% Display the array of zeros
disp('The array of zeros is: ');
disp(A);

輸出

The array of zeros is: 
     0     0     0     0     0     0

建立大小為 n x 1 或零列向量的零陣列

我們可以在 MATLAB 程式設計中建立大小為 n x 1 的列向量或大小為 n x 1 的零陣列。

以下 MATLAB 程式演示了建立大小為 6 x 1 的零陣列。

示例

% MATLAB program to demonstrate the creation of an n x 1 array of zeros
% Creating an array of zeros of size 6 x 1
A = [0; 0; 0; 0; 0; 0];
% Display the array of zeros
disp('The array of zeros is: ');
disp(A);

輸出

The array of zeros is: 
     0
     0
     0
     0
     0
     0

建立大小為 n x m 的零陣列

我們可以在 MATLAB 程式設計中建立大小為 n x m 的零陣列。其中,`n` 是陣列的行數,`m` 是陣列的列數。

以下 MATLAB 程式演示了建立大小為 n x m 的零陣列。

示例

% MATLAB program to demonstrate the creation of an n x m array of zeros
% Creating an array of zeros of size 3 x 5
A = [0 0 0 0 0; 0 0 0 0 0; 0 0 0 0 0];
% Display the array of zeros
disp('The array of zeros is: ');
disp(A);

輸出

The array of zeros is: 
0     0     0     0     0
0     0     0     0     0
0     0     0     0     0

結論

總之,我們可以透過手動或使用內建函式“zeros()”在 MATALB 中建立零陣列。以上 MATLAB 程式演示了建立零陣列的兩種方法。

更新於: 2023-07-18

211 次瀏覽

啟動你的 職業生涯

透過完成課程獲得認證

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