Creation of matrices in Matlab
In Matlab, all variables are treated as matrices. Even a scalar is a 1x1 matrix.
Basic Matrices
MATLAB incorporates a series of functions to create matrices: (Consult the documentation)
- eye(n) nxn dimension identity matrix
- rand(m,n) Random value matrix in [0,1] of m rows and n columns
- ones(m,n) Constant matrix with all elements equal to 1
- magic(n) nxn dimension matrix where all rows and columns add up the same
- diag(v) Diagonal matrix with vector v as its diagonal
- hilb(n) nxn dimension Hilbert matrix
A=eye(4)+ones(4)+hilb(4)
3.0000 1.5000 1.3333 1.2500
1.5000 2.3333 1.2500 1.2000
1.3333 1.2500 2.2000 1.1667
1.2500 1.2000 1.1667 2.1429
matIdent=A*B %notice the suspicious -0.0000 (????)
1.0000 -0.0000 -0.0000 0
-0.0000 1.0000 0.0000 0
0.0000 -0.0000 1.0000 0
0.0000 -0.0000 0 1.0000
Exercise 1
Change the format of displaying numbers in Matlab (by default is format short) by typing in the command line. Evaluate the result (the most important value is the exponent).
fLong = A*B %we still see the -0.00000000
1.000000000000000 -0.000000000000000 -0.000000000000000 0
-0.000000000000000 1.000000000000000 0.000000000000000 0
0.000000000000000 -0.000000000000000 1.000000000000000 0
0.000000000000000 -0.000000000000000 0 1.000000000000000
fShortE = A*B %now we see they are really small values (notice the exponent!!)
1.0000e+00 -1.6653e-16 -1.1102e-16 0
-2.7756e-17 1.0000e+00 5.5511e-17 0
8.3267e-17 -1.1102e-16 1.0000e+00 0
5.5511e-17 -1.1102e-16 0 1.0000e+00
fLongE = A*B %finally we see all significant figures (but what counts is the exponent!!)
1.000000000000000e+00 -1.665334536937735e-16 -1.110223024625157e-16 0
-2.775557561562891e-17 1.000000000000000e+00 5.551115123125783e-17 0
8.326672684688674e-17 -1.110223024625157e-16 1.000000000000000e+00 0
5.551115123125783e-17 -1.110223024625157e-16 0 9.999999999999999e-01
format short %we recover the original format
Cycle through the elements of a matrix
The elements in a matrix will almost always be cycled through with a for instruction because we always know the total number of iterations in the loop
A(i,j)=1; %constant value
error1 = A-B %we check if they give the same
Sum by rows and columns
% Now we want to sum rows in A in two ways
% by element (long method!!):
sumaF=zeros(m,1); % A vector of m rows and 1 colum that will contain the sum
sumaF(i)=0; %Initially the sum of the row i is 0
sumaF(i)=sumaF(i)+A(i,j);
sumFila=sum(A,2); %The sum is made by varying the second component (sum by rows)
error2=sumaF-sumFila %We verify if it gives the same
Exercise 2
Do the same but with the sum by columns. Apply this to a matrix created with the magic instruction.
Functions for handling matrices: diag, tril, triu
To define or remove information from a matrix, we may use these Matlab functions (see documentation)
A=diag(v) %Diagonal matrix with vector v as diagonal
4 0 0 0
0 2 0 0
0 0 5 0
0 0 0 1
Adsup=diag(v(1:3),1) %First diagonal above (only 3 items)
0 4 0 0
0 0 2 0
0 0 0 5
0 0 0 0
Adinf=diag(v(1:3),-1) %The first diagonal below
0 0 0 0
4 0 0 0
0 2 0 0
0 0 5 0
Other possibilities
A = magic(5)
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
ALD = tril(A) %Matrix with the lower triangular part of A (diagonal included)
17 0 0 0 0
23 5 0 0 0
4 6 13 0 0
10 12 19 21 0
11 18 25 2 9
AL = tril(A,-1) %without the diagonal
0 0 0 0 0
23 0 0 0 0
4 6 0 0 0
10 12 19 0 0
11 18 25 2 0
AUD = triu(A) %Matrix with the upper triangular part of A (diagonal included)
17 24 1 8 15
0 5 7 14 16
0 0 13 20 22
0 0 0 21 3
0 0 0 0 9
AU = triu(A,1) %without the diagonal
0 24 1 8 15
0 0 7 14 16
0 0 0 20 22
0 0 0 0 3
0 0 0 0 0
D = diag(A) %vector with the diagonal of A
AD = diag(diag(A)) %diagonal matrix with the diagonal from A
17 0 0 0 0
0 5 0 0 0
0 0 13 0 0
0 0 0 21 0
0 0 0 0 9
A-(AU+AD+AL) %we check that it is the same
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
Example of building a matrix
The matrix
It can be built with the following instructions:
Linear Algebra: rank, det, inv, eig
Matlab incorporates a large set of functions to be able to study a matrix
A=eye(4)+ones(4)+hilb(4);
[rank(A), det(A)] %Calculates the range and determinant of a matrix
Ainv = inv(A) %calculates the inverse
0.5597 -0.2151 -0.1515 -0.1236
-0.2151 0.7801 -0.2077 -0.1983
-0.1515 -0.2077 0.7814 -0.2208
-0.1236 -0.1983 -0.2208 0.7700
Linear Systems
If we want to solve an Ax=B system, Matlab offers us the backlash operator \
Obs. This operator solves the system with an LU descomposition if it is determined compatible (m=n and det(A)~=0) or with a QR decomposition if it is overdetermined (m > n and rank=n) giving the minimum quadratic solution.
% example of compatible determined system
x = A\b
0.0696
0.1590
0.2015
0.2274
% we check if it gives the same (even if the calculation is different)
example of overdetermined system
Generation and visualization of a binary matrix (quite large)
If we consider a binary matrix that which only has 1 or 0 in its components, we can generate it using the rand instruction and rounding the values. To display large matrices we can use Matlab's spy function which shows only the items different from zero.
m=60; %We make a matrix sufficiently large
A=round(rand(m,n)); %when we round, the values will only be 1 or 0
spy(A); %Shows the values different from zero in a matrix and counts them
% Question: Why wouldn't it make much sense to use the functions
% A=ceil(rand(m,n)) or A=floor(rand(m,n)) ?
Binary images in Matlab
A special type of binary matrices are images with the same name, as each pixel stores only 0 or 1
Uploading an image with Matlab is very easy (pity that this workshop can't give a deeper insight into the theme of images.... :-)
imatge=imread('BinR2D2.png'); %check the path of the image just in case
imshow(imatge); %to show as an image
spy(imatge); %in fact, it is a matrix and we can visualize it
(c)Numerical Factory, 2020