Main Content

empty

Create empty array of specified class

Description

This page describes the empty method, which creates empty arrays of a given class. To test if an existing array is an empty array, use isempty.

example

A = ClassName.empty returns an empty 0-by-0 array of the specified class. Replace ClassName with the actual name of the class. For more information on how empty arrays behave, see Empty Arrays in MATLAB.

example

A = ClassName.empty(sz1,...,szN) returns an empty array with the specified dimensions. At least one of the dimensions must be 0.

example

A = ClassName.empty(sizeVector) returns an empty array with the specified dimensions. At least one of the dimensions must be 0.

Examples

collapse all

Call the empty method on uint8 with no size specified.

A = uint8.empty
A =

  0x0 empty uint8 matrix

Assigning a value to the empty array expands it to a nonempty array. The value you assign to the empty array must be of the same class as the array or convertible to that class. MATLAB fills the other elements of the array with the default value of the array type, which for uint8 is 0.

A(3,3) = 5
A = 3x3 uint8 matrix

   0   0   0
   0   0   0
   0   0   5

Initializing a Nonempty Array

To initialize a nonempty array, use a function such as zeros or ones to fill the array with initial values. MATLAB does not have a null value, so all nonempty arrays must have values for all elements. You cannot use empty to create a 3-by-3 array, for example, because at least one dimension must have length 0.

MATLAB allows for empty arrays that have dimensions with nonzero sizes, as long as at least one dimension is 0. These empty arrays, such as a 0-by-5 array, can arise naturally in many iterative algorithms, and they follow the same rules as 0-by-0 empty arrays. The array has a class but does not contain any elements.

You can create empty arrays with nonzero dimension sizes as a way of documenting your code. For example, create an int16 empty array with dimensions 0-by-5.

A = int16.empty(0,5)
A =

  0x5 empty int16 matrix

Use A as the initial value for a 6-by-5 matrix of integers, built by vertical concatenation of 1-by-5 vectors.

for i = 1:6
    A = [A; randi(9,[1 5],"int16")];
end
A
A = 6x5 int16 matrix

   8   9   2   9   6
   1   3   5   9   9
   2   9   9   5   8
   2   4   9   8   9
   6   1   8   9   7
   7   7   4   6   2

Use a vector to define the dimensions of an empty array.

V = [0 0 6];
Bdouble = double.empty(V)
Bdouble =

  0x0x6 empty double array

Input Arguments

collapse all

Dimensions of array, specified as integers. At least one dimension must be 0. Negative values are treated as 0. Trailing dimensions of 1 are not included in the size of the array.

Vector of dimensions, specified as a row vector of integers. At least one element must be 0. Negative values are treated as 0. Trailing dimensions of 1 are not included in the size of the array.

Output Arguments

collapse all

Empty array, returned as an empty array of the specified class and dimensions.

More About

collapse all

Empty Arrays in MATLAB

In MATLAB®, an empty array is an array that has at least one dimension of size 0. An empty array has no elements.

The empty method enables you to initialize arrays of a specific class. You can expand an empty array into a nonempty array by assigning a specific value into the empty array. Any object you assign to the array must be of the same class or convertible to the class of the empty array. For example, create an empty array of uint8 and try to assign a string value to it.

A = uint8.empty;
A(3,3) = "word"
Unable to perform assignment because value of type 'string' is 
not convertible to 'uint8'.

Note

Because empty arrays must have at least one dimension of size 0, you cannot use empty to create a 3-by-3 array, for example. MATLAB does not have a null value, so all nonempty arrays must have values assigned to all elements. To quickly initialize a nonempty numeric array, consider using functions like zeros or ones.

Empty Arrays of a User-Defined Class

You can initialize an empty array of a user-defined class. For example, the empty static method is a hidden method of the ColorInRGB class defined here.

classdef ColorInRGB
   properties
      Color (1,3) = [1,0,0];
   end
   methods
      function obj = ColorInRGB(c)
         if nargin > 0
            obj.Color = c;
         end
      end
   end
end

Call the empty method.

A = ColorInRGB.empty;

You can expand this empty array into a nonempty array by assigning a value to it. For more information on how MATLAB fills arrays with objects, see Create and Initialize Object Arrays.

Identify Empty Arrays

You can use the isempty, size, and length functions to identify empty object arrays. For example, create an empty array of the ColorInRGB class defined in the previous section.

A = ColorInRGB.empty(0,5);
isempty(A)
ans =

  logical

   1
size(A)
ans =

     0     5
length(A)
ans =

     0

Concatenation of Empty Arrays

Empty arrays follow array concatenation behavior. For example, create an empty array of double and concatenate it to create a second array.

A = double.empty(0,5);
B = [A A]
B = 

  0×10 empty double matrix

Tips

  • empty is a hidden, public, static method of all nonabstract MATLAB classes. You can override the empty method in class definitions.

  • This method is useful for creating empty arrays of data types that do not have a special syntax for creating empty arrays, such as [] for double arrays.

Version History

Introduced in R2008a