Main Content

empty

Create empty array of specified class

Description

The empty method creates empty arrays of a given class. For an introduction to empty arrays in MATLAB®, see Empty Arrays. To test if an existing array is an empty array, use isempty.

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.

example

Examples

collapse all

Call the empty method on uint8 with no size specified.

A = uint8.empty
A =

  0×0 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 = 3×3 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 =

  0×5 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 = 6×5 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 =

  0×0×6 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

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