next up previous contents
Next: 3.2.5 Indexing multidimensional arrays Up: 3.2 Manipulating matrices and Previous: 3.2.3 Array-array operations   Contents

3.2.4 Special arrays

Some arrays come up so often that there are special ways of constructing them or manipulating them. Examples include the identity matrix, an array of zeroes or ones, and diagonal matrices. This is pretty straightforward so we'll just do one simple example of each.
% Special arrays
eye(3) % a 3x3 identity array
eye(3,5) % a 3x5 array with ones on the "diagonal"

ones(4) % 4x4 array of 1's
ones(2,3)*5 % 2x3 array of 5's

zeros(3) % 3x3 array of zeroes

rand(3,2) % 3x2 array of random numbers between 0 and 1

d = 1:4 
diag(d) % Make a diagonal array using the elements of d
diag(d,-1) % same but move the elements of d down by 1 row each

repmat(2.5,3,2) % "rep"licate "mat"rix, fastest way (in term of cpu time)

Gus Hart 2005-01-28