askvity

Which MATLAB function is used to create a matrix of all zeros?

Published in MATLAB Functions 4 mins read

The MATLAB function used to create a matrix of all zeros is zeros.

Understanding the zeros Function

The zeros function is a fundamental tool in MATLAB for initializing matrices or arrays with zero values. This is often necessary when pre-allocating space for computations or creating masks or placeholders.

As defined in the documentation, X = zeros( n ) returns an n -by- n matrix of zeros. This is the simplest form, creating a square matrix. However, the zeros function is versatile and can create zero matrices of various dimensions and data types.

Basic Syntax and Usage

The most common way to use zeros is by specifying the dimensions of the desired matrix.

  • Square Matrix: To create a square matrix with n rows and n columns.

    % Create a 3x3 matrix of zeros
    Z_square = zeros(3);

    This directly uses the form mentioned in the reference: X = zeros( n ).

  • Rectangular Matrix: To create a matrix with m rows and n columns.

    % Create a 2x4 matrix of zeros
    Z_rect = zeros(2, 4);
  • Multi-dimensional Array: To create an array with more than two dimensions.

    % Create a 2x3x2 array of zeros
    Z_3D = zeros(2, 3, 2);

Creating Zero Matrices with Specific Properties

You can also specify the data type or base the size on an existing variable.

  • Specify Data Type: Create a zero matrix with a specific data type (e.g., int8, double, logical). double is the default if not specified.

    % Create a 3x3 matrix of int8 zeros
    Z_int8 = zeros(3, 'int8');
    
    % Create a 2x2 matrix of logical zeros (false)
    Z_logical = zeros(2, 'logical');
  • Size Based on Another Array: Create a zero matrix that has the same dimensions as an existing array.

    % Assume A is an existing matrix
    A = [1 2; 3 4; 5 6];
    
    % Create a zero matrix with the same size as A
    Z_like_A = zeros(size(A));

    This is particularly useful when initializing a result matrix based on input matrix dimensions.

Summary of zeros Function Syntax

Here's a quick overview of common syntax variations:

Syntax Description Example Resulting Size
zeros(n) Creates an n-by-n square matrix of zeros. zeros(3) 3x3
zeros(m, n) Creates an m-by-n matrix of zeros. zeros(2, 5) 2x5
zeros(sz) Creates an array of zeros where sz is a row vector specifying dimensions. zeros([2, 3, 4]) 2x3x4
zeros(..., datatype) Creates a zero array with the specified data type. zeros(3, 'int8') 3x3 (int8)
zeros('size', A) Creates a zero array with the same dimensions and data type as array A. zeros('size', A) Same as A

Practical Applications

Using the zeros function is crucial for efficient MATLAB programming.

  • Pre-allocation: When you know the size of a matrix before filling it, using zeros to create it first is faster than dynamically growing the matrix element by element or row by row in a loop.

    % Good practice: pre-allocate the matrix
    myResults = zeros(100, 1);
    for i = 1:100
        myResults(i) = i * 2; % Fill the pre-allocated matrix
    end
    
    % Avoid (slow for large N): dynamic growth
    % badResults = [];
    % for i = 1:100
    %     badResults(i) = i * 2; % MATLAB has to resize array each time
    % end
  • Initializing Variables: Creating a matrix of zeros provides a clean slate for subsequent calculations.

  • Placeholders/Masks: A zero matrix can serve as a base layer to which other values are added, or as a mask where non-zero values indicate areas of interest.

By using the zeros function, you effectively create arrays pre-filled with zero values, which is a fundamental step in many numerical computations and data manipulations in MATLAB. For more details, you can refer to the official MATLAB documentation on the zeros function.

Related Articles