createArray
Syntax
Description
X = createArray creates an array of objects of a given class and
value, depending on the combination of input arguments passed to the function. When called
with no input arguments, it returns the scalar 0.
X = createArray( returns an
sz1,...,szN)sz1-by-...-by-szN array of zeros, where
sz1,...,szN indicate the size of each dimension. For example,
createArray(2,3) returns a 2-by-3 matrix of zeros.
X = createArray(___,
returns an array of default values of class classname)classname. You can use this
argument with any of the input arguments from the previous syntaxes. For example,
createArray(3,"int8") returns a 3-by-3 matrix of 8-bit integer
zeros.
X = createArray(___,
specifies options using one or more name-value arguments. You can combine name-value
arguments with any of the input arguments from the previous syntaxes, with the exception
of Name=Value)classname and the Like name-value argument,
which cannot be used together. For example,
createArray(2,3,Like=single(1+1i),FillValue=NaN) returns a 2-by-3
matrix filled with single(NaN+0i).
Examples
You can specify the dimensions of an array using one of three syntaxes.
When you specify a scalar, createArray returns a square matrix. Create a 3-by-3 array of zeros using a scalar as input.
A = createArray(3)
A = 3×3
0 0 0
0 0 0
0 0 0
You can specify the dimensions individually. Create a 2-by-3 array of zeros by specifying both dimensions.
B = createArray(2,3)
B = 2×3
0 0 0
0 0 0
You can also use a vector to specify the size of the array. Create a 4-by-3-by-2 array of zeros using a vector input.
d = [4 3 2]; C = createArray(d)
C =
C(:,:,1) =
0 0 0
0 0 0
0 0 0
0 0 0
C(:,:,2) =
0 0 0
0 0 0
0 0 0
0 0 0
When you specify a class but not a fill value, createArray fills the array with the default value of that class. Create a 1-by-5 array of class single. The array is filled with 0, which is the default value of single.
S = createArray(1,5,"single")S = 1×5 single row vector
0 0 0 0 0
createArray can return arrays of almost any MATLAB class. Create a 5-by-1 array of class datetime. The array is filled with NaT, which is the default value of datetime.
D = createArray(5,1,"datetime")D = 5×1 datetime
NaT
NaT
NaT
NaT
NaT
You can also use the Like name-value argument to specify a class. createArray uses the class of the prototype to determine a default value. Create a 1-by-3 array using a uint16 prototype. The returned array preserves the class of the prototype value and substitutes in the default value of uint16.
E = createArray(1,3,Like=uint16(12))
E = 1×3 uint16 row vector
0 0 0
The Like name-value argument enables you to create an array based on a prototype. The returned array takes on the class, complexity, sparsity, and other properties of the prototype.
Define a complex variable of class uint8.
p = uint8(7 + 3i)
p = uint8
7 + 3i
Create a 3-by-3 array using p as a prototype. The returned array has the same class and complexity as p, but createArray uses the default value of the class, which is 0, instead of the actual value of p.
X = createArray(3,Like=p)
X = 3×3 uint8 matrix
0 + 0i 0 + 0i 0 + 0i
0 + 0i 0 + 0i 0 + 0i
0 + 0i 0 + 0i 0 + 0i
Using a prototype also preserves other properties of the prototype, including general formatting. Create a 2-by-3 array with a duration prototype.
Y = createArray(2,3,Like=seconds(30))
Y = 2×3 duration
0 sec 0 sec 0 sec
0 sec 0 sec 0 sec
The value of elements of Y is the default value of duration, but they have the same format as the prototype.
Y.Format
ans = 's'
Use the FillValue name-value argument to set the value of all elements of an array.
Create a 2-by-3 array of 10 seconds as a duration.
A = createArray(2,3,FillValue=seconds(10))
A = 2×3 duration
10 sec 10 sec 10 sec
10 sec 10 sec 10 sec
createArray preserves the class of the fill value. Create a 3-by-3 array of pi as a single.
P = createArray(3,FillValue=single(pi))
P = 3×3 single matrix
3.1416 3.1416 3.1416
3.1416 3.1416 3.1416
3.1416 3.1416 3.1416
Using classname and FillValue Together
You can specify the classname and FillValue arguments at the same time, but the FillValue class must match the class specified by classname or be convertible to that class.
Create a 3-by-3 array using a duration as the fill value but specify "string" as classname. MATLAB converts the duration to a string.
f = duration(0,30,0,Format="hh:mm"); C = createArray(3,"string",FillValue=f)
C = 3×3 string
"00:30" "00:30" "00:30"
"00:30" "00:30" "00:30"
"00:30" "00:30" "00:30"
Using Like and FillValue Together
When Like and FillValue are both specified, createArray attempts to convert the FillValue to the same class, complexity, sparsity, and other properties as the prototype value.
Create a 2-by-4 matrix using a FillValue of 8 and a single, complex-valued prototype p. The returned matrix is filled with complex 8 of class single.
p = single(3 + 1i); Y = createArray(2,4,Like=p,FillValue=8)
Y = 2×4 single matrix
8.0000 + 0.0000i 8.0000 + 0.0000i 8.0000 + 0.0000i 8.0000 + 0.0000i
8.0000 + 0.0000i 8.0000 + 0.0000i 8.0000 + 0.0000i 8.0000 + 0.0000i
Create a 2-by-3 matrix by specifying a prototype datetime value with hh:mm format and a FillValue of the current time. The returned array has datetime values in the same format as the prototype.
T = datetime(NaT,Format="hh:mm"); Z = createArray(2,3,Like=T,FillValue="now")
Z = 2×3 datetime
12:16 12:16 12:16
12:16 12:16 12:16
Create an array of objects of a user-defined class named BasicClass. (For more information on this class, see Creating a Simple Class.)
classdef BasicClass
properties
Value {mustBeNumeric} = 0
end
methods
function obj = BasicClass(val)
if nargin == 1
obj.Value = val;
end
end
function r = roundOff(obj)
r = round([obj.Value],2);
end
function r = multiplyBy(obj,n)
r = [obj.Value] * n;
end
function r = plus(o1,o2)
r = [o1.Value] + [o2.Value];
end
end
end
Create a 2-by-3 object array. Use "BasicClass" as the classname input argument. createArray calls the no-argument constructor once to create a default object. The array is filled with copies of that default object.
A = createArray(2,3,"BasicClass")A=2×3 BasicClass array with properties:
Value
Because all elements are copies of the default object, the Value property of each instance takes the default property value of 0.
[A.Value]
ans = 1×6
0 0 0 0 0 0
Create a new 2-by-2 array using A as a prototype. Set the FillValue to NaN. MATLAB converts the fill value to the class of the prototype by calling the BasicClass constructor with the fill value as an argument.
B = createArray(2,Like=A,FillValue=NaN)
B=2×2 BasicClass array with properties:
Value
Because all elements are copies of the result of one call to the one-argument constructor, the Value property of each instance is assigned the value NaN.
[B.Value]
ans = 1×4
NaN NaN NaN NaN
Set the FillValue to an instance of BasicClass with an input argument to get a 2-by-2 array with the property Value set to -1.
C = createArray(2,FillValue=BasicClass(-1)); [C.Value]
ans = 1×4
-1 -1 -1 -1
Input Arguments
Size of square matrix, specified as an integer value.
If
nis0, thenXis an empty matrix.If
nis negative, then it is treated as0.
Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
Size of each dimension, specified as separate arguments of integer values.
If the size of any dimension is
0, thenXis an empty array.If the size of any dimension is negative, then it is treated as
0.Beyond the second dimension,
createArrayignores trailing dimensions with a size of1. For example,createArray(3,1,1,1)produces a 3-by-1 vector of zeros.
Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
Size of each dimension, specified as a row vector of integer values. Each element of this vector indicates the size of the corresponding dimension:
If the size of any dimension is
0, thenXis an empty array.If the size of any dimension is negative, then it is treated as
0.Beyond the second dimension,
createArrayignores trailing dimensions with a size of1. For example,createArray([3 1 1 1])produces a 3-by-1 vector of zeros.
Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
Class to create, specified as a string scalar or character vector. Valid inputs
include names of fundamental MATLAB® classes (see Fundamental MATLAB Classes) as well as user-defined classes. If you do not
specify a FillValue, the array is filled with the default value the
class uses for array expansion. For example, numeric and logical types have a default
value of zero. For other fundamental MATLAB classes, see their individual reference pages.
For user-defined classes, the default values are determined by a method call based on the type of class:
Heterogeneous hierarchy superclass — Call to
getDefaultScalarElement.Class that customizes parentheses indexing — Call to
parenAssign.Other classes — Call to no-argument constructor.
When you specify both classname and
FillValue, the value in FillValue must be
the same class as classname or convertible to that class.
MATLAB errors if classname and Like are
specified at the same time, even if the prototype is the same class or convertible to
the class specified by classname.
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN, where Name is
the argument name and Value is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Example: FillValue = "text"
Prototype of the array to create, specified as an array. The returned array has the same class, sparsity, and other array attributes as the prototype. The elements of the array take the default value of the prototype class.
Dependencies
MATLAB errors if
classnameandLikeare specified at the same time, even if the prototype is the same class or convertible to the class specified byclassname.When you specify both
LikeandFillValue, the value inFillValuemust be the same class asLikeor convertible to that class. The returned array takes the class, sparsity, and other attributes of theLikevalue, even if they are different from those of theFillValue. If either theFillValueor theLikevalue is complex, the returned array is complex.
The value to fill the array with, specified as a scalar. The value can be a fundamental MATLAB class or a user-defined class, and complex numbers are supported.
Dependencies
When you specify both
classnameandFillValue, the value inFillValuemust be the same class asclassnameor convertible to that class.When you specify both
LikeandFillValue, the value inFillValuemust be the same class asLikeor convertible to that class. The returned array takes the class, sparsity, and other attributes of theLikevalue, even if they are different from those of theFillValue. If either theFillValueor theLikevalue is complex, the returned array is complex.
Tips
For information on customizing createArray, see Class Support for Array-Creation Functions.
Extended Capabilities
Usage notes and limitations:
The
n,sz1,…,szN, andszarguments must be non-negative and non-empty.Code generation does not support object arrays. You can use
createArrayto create scalar objects.You cannot create
table,timetable,categorical,datetime, anddlarray(Deep Learning Toolbox) objects usingcreateArray.If you use
createArrayto create a handle class object or to create an instance of a class that customizes parentheses indexing, the class must implement acreateArrayorcreateArrayLikemethod. See Class Support for Array-Creation Functions.Code generation does not support arrays of strings. To use
createArrayto create a scalar string, you must specify theFillValuename-value argument. For example:function out = createString out = createArray(1,1,"string",FillValue="") end
The
Likename-value argument does not support structures. For example, code generation fails for this function:To create an array of structures based on a structure prototype, use thefunction out = createArrayError s = struct('field1', 3, 'field2', false); out = createArray(3,4,Like=s); end
FillValuename-value argument. For example:Alternatively, use thefunction out = createArrayExample1 s = struct('field1', 3, 'field2', false); out = createArray(3,4,FillValue=s); end
classnameargument to create an array of empty structures. For example:function out = createArrayExample2 out = createArray(3,4,"struct"); end
This function fully supports GPU arrays. You can create a gpuArray
object by doing one of the following:
Specifying
classNameas"gpuArray".Specifying a
gpuArrayas the prototype array using theLikename-value argument.Specifying the
FillValueas agpuArraywithout specifying aclassNameor a prototype array.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
This function fully supports distributed arrays. You can create a
distributed or codistributed array by doing one of the
following:
Specifying
classNameas"distributed"or"codistributed".Specifying a
distributedorcodistributedarray as the prototype array using theLikename-value argument.Specifying the
FillValueas adistributedorcodistributedarray without specifying aclassNameor a prototype array.
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced in R2024aYou can generate code for the createArray function, with some
limitations.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)