| MATLAB Function Reference | Search  Help Desk |
| zeros | Examples See Also |
B = zeros(n) B = zeros(m,n) B = zeros([m n]) B = zeros(d1,d2,d3...) B = zeros([d1 d2 d3...]) B = zeros(size(A))
B = zeros(n)
returns an n-by-n matrix of zeros. An error message appears if n is not a scalar.
B = zeros(m,n) or B = zeros([m n])
returns an m-by-n matrix of zeros.
B = zeros(d1,d2,d3...) or B = zeros([d1 d2 d3...])
returns an array of zeros with dimensions d1-by-d2-by-d3-by-... .
B = zeros(size(A))
returns an array the same size as A consisting of all zeros.
The MATLAB language does not have a dimension statement--MATLAB automatically allocates storage for matrices. Nevertheless, most MATLAB programs execute faster if the zeros function is used to set aside storage for a matrix whose elements are to be generated one at a time, or a row or column at a time.
With n = 1000, the for loop
for i = 1:n, x(i) = i; endtakes about 1.2 seconds to execute on a Sun SPARC-1. If the loop is preceded by the statement x = zeros(1,n); the computations require less than 0.2 seconds.
eye Identity matrix
ones Create an array of all ones
rand Uniformly distributed random numbers and arrays
randn Normally distributed random numbers and arrays