# zeros

Create array of all zeros with fixed-point properties

## Description

`X = zeros('like',`

returns
a scalar `p`

)`0`

with the same `numerictype`

,
complexity (real or complex), and `fimath`

as `p`

.

`X = zeros(`

returns
an `sz1,...,szN`

,'like',`p`

)`sz1`

-by-...-by-`szN`

array of
zeros like `p`

.

## Examples

### 2-D Array of Zeros With Fixed-Point Attributes

Create a 2-by-3 array of zeros with specified numerictype and `fimath`

properties.

Create a signed `fi`

object with word length of `24`

and fraction length of `12`

.

p = fi([],1,24,12);

Create a 2-by-3 array of zeros that has the same numerictype properties as `p`

.

`X = zeros(2,3,'like',p)`

X = 0 0 0 0 0 0 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 24 FractionLength: 12

### Size Defined by Existing Array

Define a 3-by-2 array `A`

.

A = [1 4 ; 2 5 ; 3 6]; sz = size(A)

`sz = `*1×2*
3 2

Create a signed `fi`

object with word length of `24`

and fraction length of `12`

.

p = fi([],1,24,12);

Create an array of zeros that is the same size as `A`

and has the same numerictype properties as `p`

.

`X = zeros(sz,'like',p)`

X = 0 0 0 0 0 0 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 24 FractionLength: 12

### Square Array of Zeros With Fixed-Point Attributes

Create a 4-by-4 array of zeros with specified numerictype and `fimath`

properties.

Create a signed `fi`

object with word length of `24`

and fraction length of `12`

.

p = fi([],1,24,12);

Create a 4-by-4 array of zeros that has the same numerictype properties as `p`

.

`X = zeros(4, 'like', p)`

X = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 24 FractionLength: 12

### Complex Fixed-Point Zero

Create a scalar fixed-point `0`

that is not real valued, but instead is complex like an existing array.

Define a complex `fi`

object.

p = fi( [1+2i 3i],1,24,12);

Create a scalar `1`

that is complex like `p`

.

`X = zeros('like',p)`

X = 0.0000 + 0.0000i DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 24 FractionLength: 12

### Write MATLAB Code That Is Independent of Data Types

Write a MATLAB^{®} algorithm that you can
run with different data types without changing the algorithm itself.
To reuse the algorithm, define the data types separately from the
algorithm.

This approach allows you to define a baseline by running the algorithm with floating-point data types. You can then test the algorithm with different fixed-point data types and compare the fixed-point behavior to the baseline without making any modifications to the original MATLAB code.

Write a MATLAB function, `my_filter`

,
that takes an input parameter, `T`

, which is a structure
that defines the data types of the coefficients and the input and
output data.

function [y,z] = my_filter(b,a,x,z,T) % Cast the coefficients to the coefficient type b = cast(b,'like',T.coeffs); a = cast(a,'like',T.coeffs); % Create the output using zeros with the data type y = zeros(size(x),'like',T.data); for i = 1:length(x) y(i) = b(1)*x(i) + z(1); z(1) = b(2)*x(i) + z(2) - a(2) * y(i); z(2) = b(3)*x(i) - a(3) * y(i); end end

Write a MATLAB function, `zeros_ones_cast_example`

,
that calls `my_filter`

with a floating-point step
input and a fixed-point step input, and then compares the results.

function zeros_ones_cast_example % Define coefficients for a filter with specification % [b,a] = butter(2,0.25) b = [0.097631072937818 0.195262145875635 0.097631072937818]; a = [1.000000000000000 -0.942809041582063 0.333333333333333]; % Define floating-point types T_float.coeffs = double([]); T_float.data = double([]); % Create a step input using ones with the % floating-point data type t = 0:20; x_float = ones(size(t),'like',T_float.data); % Initialize the states using zeros with the % floating-point data type z_float = zeros(1,2,'like',T_float.data); % Run the floating-point algorithm y_float = my_filter(b,a,x_float,z_float,T_float); % Define fixed-point types T_fixed.coeffs = fi([],true,8,6); T_fixed.data = fi([],true,8,6); % Create a step input using ones with the % fixed-point data type x_fixed = ones(size(t),'like',T_fixed.data); % Initialize the states using zeros with the % fixed-point data type z_fixed = zeros(1,2,'like',T_fixed.data); % Run the fixed-point algorithm y_fixed = my_filter(b,a,x_fixed,z_fixed,T_fixed); % Compare the results coder.extrinsic('clf','subplot','plot','legend') clf subplot(211) plot(t,y_float,'co-',t,y_fixed,'kx-') legend('Floating-point output','Fixed-point output') title('Step response') subplot(212) plot(t,y_float - double(y_fixed),'rs-') legend('Error') figure(gcf) end

## Input Arguments

`n`

— Size of square matrix

integer value

Size of square matrix, specified as an integer value, defines the output as a square, n-by-n matrix of ones.

If

`n`

is zero,`X`

is an empty matrix.If

`n`

is negative, it is treated as zero.

**Data Types: **`double`

| `single`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

`sz1,...,szN`

— Size of each dimension

two or more integer values

Size of each dimension, specified as two or more integer values,
defines `X`

as a sz1-by...-by-szN array.

If the size of any dimension is zero,

`X`

is an empty array.If the size of any dimension is negative, it is treated as zero.

If any trailing dimensions greater than two have a size of one, the output,

`X`

, does not include those dimensions.

**Data Types: **`double`

| `single`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

`sz`

— Output size

row vector of integer values

Output size, 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 zero,

`X`

is an empty array.If the size of any dimension is negative, it is treated as zero.

If any trailing dimensions greater than two have a size of one, the output,

`X`

, does not include those dimensions.

**Example: **`sz = [2,3,4]`

defines `X`

as
a 2-by-3-by-4 array.

**Data Types: **`double`

| `single`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

`p`

— Prototype

`fi`

object | numeric variable

Prototype, specified as a `fi`

object or numeric
variable. To use the prototype to specify a complex object, you must
specify a value for the prototype. Otherwise, you do not need to specify
a value.

Complex Number Support: Yes

## Tips

Using the `b = cast(a,'like',p)`

syntax to
specify data types separately from algorithm code allows you to:

Reuse your algorithm code with different data types.

Keep your algorithm uncluttered with data type specifications and switch statements for different data types.

Improve readability of your algorithm code.

Switch between fixed-point and floating-point data types to compare baselines.

Switch between variations of fixed-point settings without changing the algorithm code.

## Version History

**Introduced in R2013a**

## Open Example

You have a modified version of this example. Do you want to open this example with your edits?

## 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.

# Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- 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)