ones(n,m)
, zeros(n,m)
, rand(n,m)
return matrices with elements 1, 0 or random numbers between
0 and 1. eye(n,m)
has diagonalelements 1, else 0,
and hilb(n)
creates the n-th degree Hilbert-matrix.
>> A=rand(1,3) A = 0.33138 0.94928 0.56824 >> B=hilb(4) B = 1 1/2 1/3 1/4 1/2 1/3 1/4 1/5 1/3 1/4 1/5 1/6 1/4 1/5 1/6 1/7The following functions are provided for matrix calculations:
diag(x)
(extracts diagonal elements), det(x)
(determinante),
eig(x)
(eigenvalues), inv(x)
(inverse),
pinv(x)
(pseudoinverse). The adjunct matrix
is created using the operator '
.
>> det(hilb(4)) ans = 1/6048000 >> M=[2 3 1; 4 4 5; 2 9 3]; >> M' ans = 2 4 2 3 4 9 1 5 3 >> eig(M) ans = [ 11.531 -3.593 1.062 ] >> inv(M) ans = 0.75 0 -0.25 4.5455E-2 -9.0909E-2 0.13636 -0.63636 0.27273 9.0909E-2
The nontrivial functions are all based on the LU-decomposition,
which is also accessible as a function call lu(x)
.
It has 2 or 3 return values, therefor the left side
of the equation must provide multiple variables, see example
below:
>> M=[2 3 1; 4 4 5; 2 9 3] >> [l,u,p]=lu(M) % 2 or 3 return values l = % left triangular matrix (perm.) 0.5 0.14286 1 1 0 0 0.5 1 0 u = % right upper triangular matrix 4 4 5 0 7 0.5 0 0 -1.5714 p = % permutation matrix 0 0 1 1 0 0 0 1 0Without preceding point the arithmetic operators function as matrix operators, e.g.
*
corresponds to matrix
and vector multiplication.
>> x=[2,1,4]; y=[3,5,6]; >> x.*y % with point ans = [ 6 5 24 ] >> x*y % without point ans = 35
If one of the arguments is a scalar datatype, the operation is repeated for each element of the other argument:
>> x=[2,1,4]; >> x+3 ans = [ 5 4 7 ]
Matrix division corresponds to multiplication by the pseudoinverse.
Using the operator \
leads to left-division,
which can be used to solve systems of linear equations:
>> M=[2 3 1; 4 4 5; 2 9 3]; >> b=[0;3;1]; >> x=M\b % solution of M*x = b x = -0.25 -0.13636 0.90909 >> M*x % control ans = 0 3 1Systems of linear equations can (and should) be solved directly with the function
linsolve(A,b)
which will be discussed in
chapter 2.13.1.