HOWTO · MATLAB
How to Create an Empty Matrix in MATLAB
This tutorial demonstrates the method to create an empty matrix using different methods in MATLAB.
On this page
Use this Octave-compatible example to see how MATLAB handles dimensions. It creates a matrix with three columns and no rows, then asks for its dimensions. The code is supplied as an example for the configured runtime; this article does not claim a MATLAB or Octave execution result.
M = ones(0, 3);
disp(size(M));
Create a 0-by-0 Empty Matrix
A matrix is empty when at least one of its dimensions has length zero. The simplest MATLAB construction is [], whose size is 0-by-0. It has neither rows nor columns, so it contains no elements. This shape is useful as an initial value when a later operation will assign or concatenate data.
The first example uses ones(0, 3) to request zero rows and three columns. The zero in the row position means there are no elements, even though the other dimension is three. MATLAB supports dimensions with length zero; the matrix does not contain three ones because there are no positions in which to store values.
Create Empty Matrices with Specified Dimensions
Use a constructor such as ones(m,n) or zeros(m,n) when you need a particular shape with a zero-length dimension. For example, ones(0,3) describes a 0-by-3 matrix, while zeros(4,0) describes a 4-by-0 matrix. In either case, one dimension is zero, so the result has no elements. These forms can be handy when building a result incrementally or when a function expects a known number of columns.
The order of the arguments determines the displayed shape: ones(0,3) has zero rows and three columns, whereas ones(3,0) has three rows and zero columns. Do not replace the zero dimension with a positive size unless you intend to allocate positions in the matrix. The constructions here are MATLAB syntax; the article provides no claimed runtime output.
Distinguish Empty Matrices from Matrices of Zeros
zeros(3,5) creates a 3-by-5 matrix containing fifteen elements, each with value zero. It is not empty: both dimensions are positive and its elements can be read or assigned. By contrast, zeros(3,0) has no elements because its second dimension has length zero. The word “zero” therefore describes two different things in these examples: an element value of 0, or a dimension length of 0.
Choose zeros(m,n) with positive dimensions when you need a populated array initialized to zero, such as a workspace for accumulating values. Choose a zero-length dimension when the desired shape itself has no elements. This distinction also avoids describing a zero-filled image or array as an empty matrix: those objects have allocated elements even when every value is 0.
Check a Matrix’s Size and Emptiness
Use size(A) to inspect the dimensions of a matrix and isempty(A) to test whether it has no elements. For example, size(ones(0,3)) gives the shape information for the 0-by-3 case, and isempty(ones(0,3)) tests its element count. A zero-filled 3-by-5 matrix has a positive size and is not empty, even though all(zeros(3,5) == 0) is true. The value test and the emptiness test answer different questions.
Here is a self-contained check that illustrates the distinction. It is an unexecuted example in this article; no output is asserted.
A = ones(0, 3);
Z = zeros(3, 5);
disp(size(A));
disp(isempty(A));
disp(size(Z));
disp(isempty(Z));
When debugging a shape mismatch, inspect size(A) rather than inferring dimensions from the values. A matrix can contain only zeros and still have many elements, while a zero-length dimension makes the element count zero. These checks help confirm that a constructor produced the intended shape before using the result in later calculations.