View all functions

CategoryArray: Creation
GPUYes

What does the zeros function do in MATLAB / RunMat?

zeros creates arrays filled with zeros. RunMat mirrors MATLAB semantics across the scalar, vector, matrix, and N-D forms, including 'like' and 'logical' options.

How does the zeros function behave in MATLAB / RunMat?

  • zeros() returns the scalar 0.
  • zeros(n) returns an n × n double array.
  • zeros(m, n, ...) returns a dense double array with the requested dimensions.
  • zeros(sz) accepts a size vector (row or column) and returns an array with prod(sz) elements arranged using MATLAB column-major ordering.
  • zeros(A) returns an array of zeros with the same size (and device residency) as A.
  • zeros(___, 'logical') returns a logical array instead of double precision.
  • zeros(___, 'like', prototype) matches the device residency and numeric/logical flavour of prototype.

zeros Function GPU Execution Behaviour

When the prototype or 'like' argument is a GPU tensor, RunMat asks the active acceleration provider to allocate a zero-filled buffer in-place. Providers that do not yet support the zero allocation hooks automatically fall back to uploading a host zero tensor, guaranteeing correct behaviour at the cost of an extra transfer.

Examples of using the zeros function in MATLAB / RunMat

Creating a 2x3 matrix of zeros

A = zeros(2, 3);

Expected output:

A = [0 0 0; 0 0 0];

Creating a 4x1 logical matrix of zeros

mask = zeros(4, 1, 'logical');

Expected output:

mask = [0 0 0 0];

Creating a 128x128 matrix of zeros on a GPU

In RunMat:

H = zeros(128, 128);

In MathWorks MATLAB (supported in RunMat as well):

H = gpuArray(zeros(128, 128));

% OR:

G = gpuArray(rand(128, 128));
H = zeros(128, 128, 'like', G);

In both cases, the expected output is:

H = [0 0 0 ... 0; 0 0 0 ... 0; ...; 0 0 0 ... 0];

GPU residency in RunMat (Do I need gpuArray?)

You usually do NOT need to call gpuArray yourself in RunMat (unlike MATLAB).

In RunMat, the fusion planner keeps residency on GPU in branches of fused expressions. As such, in the above example, the result of the zeros call will already be on the GPU when the fusion planner has detected a net benefit to operating the fused expression it is part of on the GPU.

To preserve backwards compatibility with MathWorks MATLAB, and for when you want to explicitly bootstrap GPU residency, you can call gpuArray explicitly to move data to the GPU if you want to be explicit about the residency.

Since MathWorks MATLAB does not have a fusion planner, and they kept their parallel execution toolbox separate from the core language, as their toolbox is a separate commercial product, MathWorks MATLAB users need to call gpuArray to move data to the GPU manually whereas RunMat users can rely on the fusion planner to keep data on the GPU automatically.

FAQ

When should I use the zeros function?

Use zeros whenever you need to create arrays pre-filled with the value 0, such as for initializing weights, creating masks, or testing other algorithms. Preallocating with zeros ensures correct type and shape throughout your code and helps prevent bugs caused by uninitialized arrays.

Does zeros produce double arrays by default?

Yes, by default, zeros creates dense double-precision arrays unless you explicitly specify a type such as 'logical' or use the 'like' argument to match a prototype array.

What does zeros(n) return?

zeros(n) returns an n × n dense double-precision matrix filled with zeros. For example, zeros(3) yields a 3-by-3 matrix of all zeros.

How do I create a logical array of zeros?

Pass 'logical' as the last argument:

mask = zeros(5, 1, 'logical');

This produces a 5x1 logical array, i.e., all elements have value false (0 in binary).

How do I match the type and device residency of an existing array?

Use the 'like', prototype syntax:

A = gpuArray(rand(2,2));
B = zeros(2, 2, 'like', A);

B will be a GPU array with the same type and shape as A.

Can I create N-dimensional arrays with zeros?

Yes! Pass more than two dimension arguments (or a size vector):

T = zeros(2, 3, 4);

This creates a 2×3×4 tensor of zeros.

How does zeros(A) behave?

If you call zeros(A), where A is an array, the result is a new array of zeros with the same shape as A.

Is the output always dense?

Yes. zeros always produces a dense array. For sparse matrices of zeros, use sparse with appropriate arguments.

What if I call zeros with no arguments?

zeros() returns the scalar value 0.

Can I use zeros to preallocate arrays for later assignment?

Absolutely. Preallocating with zeros (or ones) and then filling in values is a recommended practice for efficiency and code clarity when the final values are known to overwrite the initial zeros.

See Also

ones, eye, gpuArray, gather

Source & Feedback