ones

ones(m)

ones(m, n)

ones(m, n, p, ...)

ones([m])

ones([m, n])

ones([m, n, p, ...])

Try it yourself:


ones(): create a matrix filled with one

ones(n), or ones with dimensions, creates an array or matrix whose entries are all 1. It is a convenient starting point for weights, offsets, test fixtures, and matrix formulas.

Examples

ones(3) creates a length-three vector of ones; supplying two dimensions creates a rectangular matrix. Add or multiply it with another matrix when every position needs the same adjustment.

Matrix companions

Use zeros() for a zero-filled structure, identity() for an identity matrix, and size() to verify shape. Combine with multiply() or add() in matrix expressions.

Shape matters

Dimensions should be non-negative integers. Match shapes before elementwise operations to avoid broadcasting or dimension errors.

All functions