empty - Create empty array of specified class - MATLAB (original) (raw)
Create empty array of specified class
Syntax
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](#mw%5Ff3ab9a06-9bb9-4353-86fe-d70478376540) = ClassName.empty
returns an empty 0-by-0 array of the specified class. ReplaceClassName
with the actual name of the class. For more information on how empty arrays behave, see Empty Arrays in MATLAB.
[A](#mw%5Ff3ab9a06-9bb9-4353-86fe-d70478376540) = ClassName.empty([sz1,...,szN](#mw%5F8be39176-e92c-4316-81bd-79476d23ebea))
returns an empty array with the specified dimensions. At least one of the dimensions must be 0.
[A](#mw%5Ff3ab9a06-9bb9-4353-86fe-d70478376540) = ClassName.empty([sizeVector](#mw%5F23c7a9ce-c991-4c9f-a59c-4be0ebaa284b))
returns an empty array with the specified dimensions. At least one of the dimensions must be 0.
Examples
Call the empty
method on uint8
with no size specified.
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 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 =
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
Dimensions of array, specified as integers. At least one dimension must be0
. Negative values are treated as0
. 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 as0
. Trailing dimensions of 1
are not included in the size of the array.
Output Arguments
Empty array, returned as an empty array of the specified class and dimensions.
More About
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.
You can initialize an empty array of a user-defined class. For example, theempty
static method is a hidden method of theColorInRGB
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.
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.
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)
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 theempty
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