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)
A=eye(4)+ones(4)+hilb(4)
A = 4×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
B=inv(A);
matIdent=A*B %notice the suspicious -0.0000 (????)
matIdent = 4×4
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).
format long
fLong = A*B %we still see the -0.00000000
fLong = 4×4
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
format short e
fShortE = A*B %now we see they are really small values (notice the exponent!!)
fShortE = 4×4
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
format long e
fLongE = A*B %finally we see all significant figures (but what counts is the exponent!!)
fLongE = 4×4
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
m = 4; %rows
n = 3; %columns
A=zeros(m,n);
for i=1:m %row index
for j=1:n %column index
A(i,j)=1; %constant value
end
end
B=ones(m,n);
error1 = A-B %we check if they give the same
error1 = 4×3
0 0 0 0 0 0 0 0 0 0 0 0

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
for i=1:m
sumaF(i)=0; %Initially the sum of the row i is 0
for j=1:n
sumaF(i)=sumaF(i)+A(i,j);
end
end
%
% Using Matlab Tools
%
sumFila=sum(A,2); %The sum is made by varying the second component (sum by rows)
% See the sum function
error2=sumaF-sumFila %We verify if it gives the same
error2 = 4×1
0 0 0 0

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)
v=[4,2,5,1];
A=diag(v) %Diagonal matrix with vector v as diagonal
A = 4×4
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)
Adsup = 4×4
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
Adinf = 4×4
0 0 0 0 4 0 0 0 0 2 0 0 0 0 5 0
Other possibilities
A = magic(5)
A = 5×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)
ALD = 5×5
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
AL = 5×5
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)
AUD = 5×5
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
AU = 5×5
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
D = 5×1
17 5 13 21 9
AD = diag(diag(A)) %diagonal matrix with the diagonal from A
AD = 5×5
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
ans = 5×5
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:
v1=1:10; v2=4:2:20; v3=-3*ones(1,10;
A=diag(v1) + diag(v2,+1) + diag(v3,-1);
A(1,10) = -50; A(10,1)=25;

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
ans = 1×2
4.0000 8.5902
Ainv = inv(A) %calculates the inverse
Ainv = 4×4
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
b = ones(4,1);
x = A\b
x = 4×1
0.0696 0.1590 0.2015 0.2274
% we check if it gives the same (even if the calculation is different)
xx=Ainv*b;
x-xx
ans = 4×1
10-16 ×
0.6939 0.5551 0 -0.2776
example of overdetermined system
B=[ones(5,1),(1:5)'];
b=(1:2:10)';
y=B\b
y = 2×1
-1.0000 2.0000

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
n=40;
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
[m,n]=size(imatge)
m = 361
n = 244
imshow(imatge); %to show as an image
spy(imatge); %in fact, it is a matrix and we can visualize it
(c)Numerical Factory, 2020