## 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'* 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)
```

```>> 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')
```