在 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.