Matlab - matrix properties

number format:

>> format short  % 4 decimal places (default)
>> format long   % 14 decimal places
>> format rat    % fractions

>> M = [1  2.7 -3; 4  5  1]

rank:

>> h = rank(M)

Matrix (or vector) norms:

- for every norm, there is a set of equivalent commands how to obtain it

column norm
>> max(sum(abs(M)))
>> norm(M,1)  
row norm
>> max(sum(abs(M')))
>> norm(M,inf)
Frobenius norm:
>> sqrt(sum(diag(M'* M)))
>> sqrt(trace(M))
>> norm(M,'fro')
spectral norm - max. singular value
>> max(sqrt(eig(M'* M)))
>> max(svd(M))
>> norm(M)     
>> norm(M,2) 


Properties of square matrices:

>> A = [0.5  1 ; -1  0 ]

determinant:

>> d = det(A)

trace of the matrix (sum of diagonal elements):

>> tr = sum(diag(A))
>> tr = trace(A)

eigenvalues and spectral radius:

>> lambda = eig(A)         % eigenvalues
>> alambda = abs(lambda)   % absolute values of eigenvalues
>> sp = max(alambda)       % spectral radius

>> sp = max(abs(eig(A)))   % all in one command

Matlab function for computation of spectral radius:

function sp = sp_rad(A)
  sp =  max(abs(eig(A)));
end 

eigenvalues graphically:

>> lambda = eig(A);                % eigenvalues
>> r = max(abs(lambda));           % computing spectral radius r
>> t = 0 : 0.2 : 2*pi+0.2 ;        % parameter for a circle
>> X = r*[sin(t);cos(t)];          % points on a circle with radius r
>> plot(X(1,:),X(2,:))             % plot the circle
>> hold on                         % continue on the same figure
>> plot(real(lambda),imag(lambda),'r*',0,0,'b+') % plot the eigenvalues
>> axis square;                    % the same scale on both axes
>> axis([ -r-1  r+1  -r-1  r+1 ])  % define span of axes - optional

checking if matrix is symmetric: A = AT:

>> norm(A-A')       % = 0

checking if matrix A is positive definite (suitable for small matrices only):
- we can check if all minors are positive:

>> A(1,1)           % > 0 
>> det(A(1:2,1:2))  % > 0
>> det(A(1:3,1:3))  % > 0
>> ...
>> det(A)           % > 0

- or, for symmetric matrix, check if all eigenvalues are positive:

>> eig(A)           % > 0


2D transformations:

x-axis scaling

>> Dx = [ 2 0; 0 1]

y-axis scaling

>> Dy = [ 1 0; 0 3]

scaling in both axes

>> Dxy = Dx * Dy; % composition of the transformations

mirroring by y-axis

>> Zy = [ -1 0; 0 1]

rotation of 90 degrees

>> R90 = [ 0 -1; 1 0]

rotation of alpha

alpha = - pi/6; % radians
Ralpha = [ cos(alpha) -sin(alpha); sin(alpha) cos(alpha)]

shear

>> SH = [ 1 1; 0 1]

symetric transformation

>> S = [ 3 -1; -1 2]

some general transformation

>> A = [1 2; 3 -1.5]

Displaying of 2D transformations:

picture of a house

>> X=[ 0 6 6 5 5 4 4 3 0 0;
       0 0 5 6 8 8 7 8 5 0]; 
>> hold off;
>> plot(X(1,:),X(2,:))
>> axis equal;

transformation of the picture using matrix A

A = [ 1 1; 0 1];
B = A*X;
hold on;
plot(B(1,:),B(2,:), 'r')

Function for displaying of 2D transformations:

function s = elip(A)
%
% Graphical representation of a 2D transformation A
%
% function call:  s = elip(A);  or  elip(A);
%
% input: A ... 2x2 matrix with real nonzero eigenvalues
%
% output: s ... spectral radius of A 
%
%   fig. 1 - before transformation:
%              unit circle and unit square (blue)
%              eigenvectors (red)
%              a few other vectors (different colors)
%   fig. 2 - after transformation
%              (dashed circle is spectral radius of A)


close all

% chceck if eigenvalues are real
if ( (A(1,1)-A(2,2))^2 + 4*A(2,1)*A(1,2) ) < 0
  s = 0; % imaginary eigenvectors - will not be displayed 
  disp('eigenvectors are imaginary')
else
  s = 1; % auxiliary value
end

% X - unit circle
t = 0:0.2:2*pi+0.2;
X = [sin(t);cos(t)];
hold off
plot(X(1,:),X(2,:))
axis equal;
hold on;

% T - unit square
T = [-1 1 1 -1 -1; -1 -1 1 1 -1];
plot(T(1,:),T(2,:))
axis([ -1.3  1.3  -1.1  1.1 ])

% V - eigenvectors, E - eigenvalues of A
[V,E] = eig(A);

if (s > 0)
  % plot the eigenvectors
  UP = [ [0;0] V(:,1),  [0;0] V(:,2) ];
  plot(UP(1,:),UP(2,:),'r')
end

% a, b, c, d - different unit vectors given by 2 points
t = 0.3:0.6:0.8*pi; % angles of the vectors
U = [cos(t);sin(t)]; % endpoints of the vectors
a = [ [0;0] U(:,1)];
b = [ [0;0] U(:,2)];
c = [ [0;0] U(:,3)];
d = [ [0;0] U(:,4)];
plot(a(1,:), a(2,:));
plot(b(1,:), b(2,:), 'k');
plot(c(1,:), c(2,:), 'g');
plot(d(1,:), d(2,:), 'm');


figure(2);

% transformation of the unit circle
B = A*X;
plot(B(1,:),B(2,:))
axis equal;
hold on

% transformation of the unit square
B = A*T;
plot(B(1,:),B(2,:))

if (s > 0)
  % plot transformation of the eigenvectors
  UP = A*UP;
  plot(UP(1,:),UP(2,:),'r')
end

% transformation of the other vectors
a = A*a;  b = A*b;  c = A*c;  d = A*d;
plot(a(1,:), a(2,:));
plot(b(1,:), b(2,:), 'k');
plot(c(1,:), c(2,:), 'g');
plot(d(1,:), d(2,:), 'm');

% spectral radius
s = max(abs(diag(E)));
plot(s*X(1,:),s*X(2,:), '--') % ':'

end