Qu

zeros

Builtin index · Core maths

zeros(n) / zeros(r, c)

Input: n · Output: no output

What it is

A vector or matrix of zeros. One numeric argument n gives a length-n Vec; two arguments r, c give an r×c Mat — even a degenerate two-argument shape like zeros(n, 1) stays a genuine oriented matrix, distinct from the plain Vec that zeros(n) returns. Overloaded by argument type: zeros(filt), where filt is a "filter" model (from butter/fir1/etc.), instead returns that filter's zeros as a CVec (see [Signal Processing & Filters](signal-processing.md)).

Overloads

n can be vector, matrix, Vec, Mat, CVec, or Signal; r can be Mat, matrix, or Vec; c can be Mat, matrix, or Vec; filt can be vector, matrix, Vec, Mat, CVec, Signal, or Model.

3 forms. Each carries its own inputs, outputs and worked example — pick the tab for the one you are calling.

CodeInputOutputDescription
result = zeros(n)
n vector, matrix, Vec, Mat, CVec, or Signalnot documented yet
result vector
A length-n real vector of 0.0s — the plain array constructor.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

results = zeros(5)
parallel for i in 0 to 4
    results[i] = i * i
end parallel
print(results)

from tour

print(zeros(3))                              # [0, 0, 0]

print(zeros(2, 3))                           # [0, 0, 0; 0, 0, 0]

filt = butter(4, "low", 100, 1000)
print(zeros(filt))                           # [-1, -1, -1, -1] -- a CVec, not a fresh zero-fill

output
[0, 0, 0]
[0, 0, 0; 0, 0, 0]
[-1, -1, -1, -1]

from core-math

v = zeros(4)
m = zeros(2, 3)
fs = 1000
lp = butter(2, "low", 100, fs)
zf = zeros(lp)
print("vector: {v}")             # vector: [0, 0, 0, 0]

print("matrix shape: {shape(m)}")  # matrix shape: [2, 3]

print("filter zeros: {zf}")      # filter zeros: [-1, -1]

output
vector: [0, 0, 0, 0]
matrix shape: [2, 3]
filter zeros: [-1, -1]

from signal-processing

heavy(n) := n * n

w = spawn("heavy", 7)
print("spawned result {join(w)}")

results = zeros(10)
parallel for i in 0 to 9
    results[i] = heavy(i)
end parallel
print("parallel results {results}")
output
spawned result 49
parallel results [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

from specialized

CodeInputOutputDescription
result = zeros(r, c)
r Mat, matrix, or Vecnot documented yet
c Mat, matrix, or Vecnot documented yet
result matrix
An (r, c) real matrix of 0.0s.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

print(zeros(3))                              # [0, 0, 0]

print(zeros(2, 3))                           # [0, 0, 0; 0, 0, 0]

filt = butter(4, "low", 100, 1000)
print(zeros(filt))                           # [-1, -1, -1, -1] -- a CVec, not a fresh zero-fill

output
[0, 0, 0]
[0, 0, 0; 0, 0, 0]
[-1, -1, -1, -1]

from core-math

v = zeros(4)
m = zeros(2, 3)
fs = 1000
lp = butter(2, "low", 100, fs)
zf = zeros(lp)
print("vector: {v}")             # vector: [0, 0, 0, 0]

print("matrix shape: {shape(m)}")  # matrix shape: [2, 3]

print("filter zeros: {zf}")      # filter zeros: [-1, -1]

output
vector: [0, 0, 0, 0]
matrix shape: [2, 3]
filter zeros: [-1, -1]

from signal-processing

# A gradient with a bright bar in it, so the rotation below is visible:

# a symmetric test image would look identical afterwards.

n = 48
g = zeros(n, n)
for i in 0 to n - 1
    for j in 0 to n - 1
        g[i, j] = 40 + 3 * j
        if i > 10 and i < 22 and j > 6 and j < 30
            g[i, j] = 240
        end if
    end for
end for
save_image("sample.bmp", image_from_matrix(g))

img = load_image("sample.bmp")
gray = grayscale(img)
imshow(gray)

from images

w = 120
h = 120
M = zeros(h, w)
for r in 0 to h - 1
    for c in 0 to w - 1
        M[r, c] = 40 + 60 * sin(c / 9) * cos(r / 14)
    end for
end for
for r in 45 to 75
    for c in 30 to 60
        M[r, c] = 210
    end for
end for

img = image_from_matrix(M)
imshow(img)
title("a synthetic field with one bright patch")
a synthetic field with one bright patch

from images

CodeInputOutputDescription
result = zeros(filt)
filt vector, matrix, Vec, Mat, CVec, Signal, or Modelnot documented yet
result CVec or Model
A CVec of the filter Model's zeros (IIR: each section's b0*z^2+b1*z+b2=0 roots; FIR: the tap polynomial's roots) — nothing to do with array construction at all.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

results = zeros(5)
parallel for i in 0 to 4
    results[i] = i * i
end parallel
print(results)

from tour

print(zeros(3))                              # [0, 0, 0]

print(zeros(2, 3))                           # [0, 0, 0; 0, 0, 0]

filt = butter(4, "low", 100, 1000)
print(zeros(filt))                           # [-1, -1, -1, -1] -- a CVec, not a fresh zero-fill

output
[0, 0, 0]
[0, 0, 0; 0, 0, 0]
[-1, -1, -1, -1]

from core-math

v = zeros(4)
m = zeros(2, 3)
fs = 1000
lp = butter(2, "low", 100, fs)
zf = zeros(lp)
print("vector: {v}")             # vector: [0, 0, 0, 0]

print("matrix shape: {shape(m)}")  # matrix shape: [2, 3]

print("filter zeros: {zf}")      # filter zeros: [-1, -1]

output
vector: [0, 0, 0, 0]
matrix shape: [2, 3]
filter zeros: [-1, -1]

from signal-processing

heavy(n) := n * n

w = spawn("heavy", 7)
print("spawned result {join(w)}")

results = zeros(10)
parallel for i in 0 to 9
    results[i] = heavy(i)
end parallel
print("parallel results {results}")
output
spawned result 49
parallel results [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

from specialized

How it works

constructors: zeros(n)/zeros(r, c); a genuine 2-D shape gives a matrix, a single dimension (or a k x 1 / 1 x k) gives a vector. zeros(filt) -- overloaded on argument type, not a separate name: when the (sole) argument is a "filter" model, this is instead the filter's zeros (IIR: each section's b0*z^2+b1*z+b2=0 roots; FIR: the whole tap polynomial's roots via the companion-matrix eigenvalue method, reusing eig rather than a bespoke root-finder). Matches MATLAB's own zeros/tf2zpk naming without a second builtin name for the same concept poles(filt) already established.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.