Below a list of commands to produce useful matrices.
It generates a matrix of 1’s M by N.
>> oneMatrix = ones(M,N);
It generates a matrix of 0’s M by N.
>> zeroMatrix = zeros(M,N);
It generates an identity matrix (ones in the diagonal, zeros otherwise) M by N.
>> IMatrix = eye(M,N);
>> x = [ 1 2; 3 4] >> y = [ x x x; x x x]
or with repmat,
>> y = repmat(x,2,3)
it transforms a matrix with one set of dimensions and to one with a different set, preserving the number of elements. Suppose we have an M by N matrix x and we want to transform it into an K by L matrix y as long as MN = K L, natural condition to transform is that the number of elements does not change.
>> x = [1 2; 3 4]; >> y = reshape(x,4,1) y = 1 3 2 4 >> z = reshape(y,1,4) z = 1 3 2 4 >> w = reshape(z,2,2) w = 1 2 3 4
The crucial detail to remember when using reshape is that MATLAB will always use the column-major notation to determine the shape of the new matrix. MATLAB always counts down then across, and will place elements of the old matrix into their same position in the new matrix. In other words, x(1) = y (1), x(2) = y (2), and so on.
- If the input is a square matrix, it will return a column vector of the elements along the diagonal of a matrix.
- If the input is a vector, it will return a matrix with the elements of the diagonal along the vector.
>> x = [1 2; 3 4]; x = 1 2 3 4 >> y = diag(x) y = 1 4 >> z=diag(y) z = 1 0 0 4
det(x) computes the determinant of a square matrix x.
trace(x), the trace of a square matrix x (sum of diagonal elements) equivalent to sum(diag(x))
R = chol(A) produces an upper triangular matrix R from the diagonal and upper triangle of matrix A, satisfying the equation R’*R=A. Matrix A must be positive definite.
inv(x) compute the inverse of the square matrix x, is the equivalente of x^(-1).
eig(x) computes the eigenvalues and eigenvector of a square matrix, [eigvec,eigval]=eig(x).