MATLAB Matrix-Building Functions

Some of MATLAB's convenient matrix-building functions are:

        eye       identity matrix
        zeros     matrix of zeros
        ones      matrix of ones
        diag      see below
        triu      upper triangular part of a matrix
        tril      lower triangular part of a matrix
        rand      randomly generated matrix
        hilb      Hilbert matrix
        magic     magic square
        toeplitz  see help toeplitz

For example, zeros(m,n) produces an m-by-n matrix of zeros, and zeros(n) produces an n-by-n one; if A is a matrix, then zeros(A) produces a matrix of zeros of the same size as A.

If x is a vector, diag(x) is the diagonal matrix with x down the diagonal; if A is a square matrix, then diag(A) is a vector consisting of the diagonal of A. Try to calculate the result of diag(diag(A).

Matrices can be built from blocks. For example, if A is a 3-by-3 matrix, then

     B = [A, zeros(3,2); zeros(2,3), eye(2)]

will build a certain 5-by-5 matrix. Try it.


Other Matlab Hints

Click on an item listed below to access additional MATLAB-related hints.


Return to ACS home page.


Please send comments and suggestions to

consult@rpi.edu