% Manualni prevod matice na schodovity tvar:
load Ho4-4-B1b
A

A =

     2    -4     8     0     4
     3    -6     1     4    -3
    -4     2     5    -1     7
     5    -4   -12     5   -14

% Eliminace 1.sloupce:
A1=A

A1 =

     2    -4     8     0     4
     3    -6     1     4    -3
    -4     2     5    -1     7
     5    -4   -12     5   -14

A1(2,:)=A1(2,:)-(3/2)*A(1,:)

A1 =

     2    -4     8     0     4
     0     0   -11     4    -9
    -4     2     5    -1     7
     5    -4   -12     5   -14

A1(3,:)=A1(3,:)+2*A(1,:)

A1 =

     2    -4     8     0     4
     0     0   -11     4    -9
     0    -6    21    -1    15
     5    -4   -12     5   -14

A1(4,:)=A1(4,:)-(5/2)*A(1,:)

A1 =

     2    -4     8     0     4
     0     0   -11     4    -9
     0    -6    21    -1    15
     0     6   -32     5   -24

% Zamena 2. a 4.radku
A2=A1

A2 =

     2    -4     8     0     4
     0     0   -11     4    -9
     0    -6    21    -1    15
     0     6   -32     5   -24


A2([4,2],:)=A2([2,4],:)

A2 =

     2    -4     8     0     4
     0     6   -32     5   -24
     0    -6    21    -1    15
     0     0   -11     4    -9

% Eliminace 2.sloupce
A3=A2

A3 =

     2    -4     8     0     4
     0     6   -32     5   -24
     0    -6    21    -1    15
     0     0   -11     4    -9


A3(3,:)=A3(3,:)+A3(2,:)

A3 =

     2    -4     8     0     4
     0     6   -32     5   -24
     0     0   -11     4    -9
     0     0   -11     4    -9

% Eliminace 3. sloupce

A4=A3

A4 =

     2    -4     8     0     4
     0     6   -32     5   -24
     0     0   -11     4    -9
     0     0   -11     4    -9

A4(4,:)=A4(4,:)-A4(3,:)

A4 =

     2    -4     8     0     4
     0     6   -32     5   -24
     0     0   -11     4    -9
     0     0     0     0     0

help rank

 RANK   Matrix rank.
    RANK(A) provides an estimate of the number of linearly
    independent rows or columns of a matrix A.
    RANK(A,tol) is the number of singular values of A
    that are larger than tol.
    RANK(A) uses the default tol = max(size(A)) * norm(A) * eps.

 Overloaded methods
    help sym/rank.m

diary off
% Kontrola hodnosti:

rank(A4)

ans =

     3

rank(A)

ans =

     3

%***********************************
load Ho4-4-B1a
A
A =
     0     4    10     1
     4     8    18     7
    10    18    40    17
     1     7    17     3
% Prevod na schodovity tvar pomoci predem pripravene
% procedury 'gedemo':
help gedemo

  [S,U,np,jb] = GEdemo(A)
  ----------------------------------------------------------
  Prevod na schodovity matice A Gaussovou eliminaci krok po kroku
  S  ... matice A ve schodovitem tvaru
  U  ... regularni horni trojuhelnikova submatice v S
  np ... pocet provedenych zamen radku
  jb ... radkovy vektor sloupcovych indexu baze v A
  POZNAMKA: Pri poslednim schodu staci zadat jen s misto [r,s]
            pri vstupu z klavesnice nebo nic.

[S,U,np,jb] = GEdemo(A)
m =
     4
n =
     4
A =
     0     4    10     1
     4     8    18     7
    10    18    40    17
     1     7    17     3
Vlozte polohu dalsiho schodu ve tvaru [r,s]: [4,1]
r =
     4
jb =
     1
k =
     1
PREHOZENI k-ho a r-teho RADKU:
A =
     1     7    17     3
     4     8    18     7
    10    18    40    17
     0     4    10     1
VEKTOR TRANSFORMACNICH SKALARU V s-tem SLOUPCI:
     4    10     0
ELIMINACE ZBYVAJICICH SLOUPCU s+1,...,n:
A =
     1     7    17     3
     0   -20   -50    -5
     0   -52  -130   -13
     0     4    10     1
Naposledy eliminovany sloupec:
s =
     1
Vlozte polohu dalsiho schodu ve tvaru [r,s]: [4,2]
rs =
     4     2
r =
     4
jb =
     1     2
k =
     2
PREHOZENI k-ho a r-teho RADKU:
A =
     1     7    17     3
     0     4    10     1
     0   -52  -130   -13
     0   -20   -50    -5
VEKTOR TRANSFORMACNICH SKALARU V s-tem SLOUPCI:
   -13    -5
ELIMINACE ZBYVAJICICH SLOUPCU s+1,...,n:
A =
     1     7    17     3
     0     4    10     1
     0     0     0     0
     0     0     0     0
Naposledy eliminovany sloupec:
s =
     2
Vlozte polohu dalsiho schodu ve tvaru [r,s]:
rs =
     []
% Hodnoty vystupnich promennych po skonceni procedury:
jb =
     1     2
S =
     1     7    17     3
     0     4    10     1
     0     0     0     0
     0     0     0     0
U =
     1     7
     0     4
np =
     2
jb =
     1     2

%-----------------------------------

% U teze matice provedeme nyni jeji LU-rozklad
% pomoci predem pripravene procedury 'LUdemo':
help LUdemo

  [L,U,p,jb] = LUdemo(A)
  ----------------------------------------------------------
  konstrukce LU-rozkladu matice A krok po kroku
  L  ... dolni trojuhelnikova matice
  U  ... horni trojuhelnikova (obecne schodovita) matice
  p  ... sloupcovy permutacni vektor takovy, ze A(p,:)= L*U
  jb ... radkovy vektor sloupcovych indexu baze v A
  POZNAMKA: Pri poslednim schodu staci zadat jen s misto [r,s]
            pri vstupu z klavesnice nebo nic.

[L,U,p,jb] = LUdemo(A)
m =
     4
n =
     4
A =
     0     4    10     1     1
     4     8    18     7     2
    10    18    40    17     3
     1     7    17     3     4
Vlozte polohu dalsiho schodu ve tvaru [r,s]: [4,1]
r =
     4
jb =
     1
k =
     1
PREHOZENI k-ho a r-teho RADKU:
A =
     1     7    17     3     4
     4     8    18     7     2
    10    18    40    17     3
     0     4    10     1     1
ULOZENI TRANSFORMACNICH SKALARU MISTO NUL DO s-teho SLOUPCE:
A =
     1     7    17     3     4
     4     8    18     7     2
    10    18    40    17     3
     0     4    10     1     1
ELIMINACE ZBYVAJICICH SLOUPCU s+1,...,n:
A =
     1     7    17     3     4
     4   -20   -50    -5     2
    10   -52  -130   -13     3
     0     4    10     1     1
Posledni eliminovany sloupec:
s =
     1
Vlozte polohu dalsiho schodu ve tvaru [r,s]: [4,2]
rs =
     4     2
r =
     4
jb =
     1     2
k =
     2
PREHOZENI k-ho a r-teho RADKU:
A =
     1     7    17     3     4
     0     4    10     1     1
    10   -52  -130   -13     3
     4   -20   -50    -5     2
ULOZENI TRANSFORMACNICH SKALARU MISTO NUL DO s-teho SLOUPCE:
A =
     1     7    17     3     4
     0     4    10     1     1
    10   -13  -130   -13     3
     4    -5   -50    -5     2
ELIMINACE ZBYVAJICICH SLOUPCU s+1,...,n:
A =
     1     7    17     3     4
     0     4    10     1     1
    10   -13     0     0     3
     4    -5     0     0     2
Posledni eliminovany sloupec:
s =
     2
Vlozte polohu dalsiho schodu ve tvaru [r,s]:
rs =
     []

% Hodnoty vystupnich promennych po skonceni procedury:
jb =
     1     2
L =
     1     0     0     0
     0     1     0     0
    10   -13     1     0
     4    -5     0     1
U =
     1     7    17     3
     0     4    10     1
     0     0     0     0
     0     0     0     0
p =
     4
     1
     3
     2
jb =
     1     2
% Pomoci vektoru p vytvorime permutacni matici
P=eye(size(A,1))
P =
     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1
P=P(p,:)
P =
     0     0     0     1
     1     0     0     0
     0     0     1     0
     0     1     0     0
% Nasledujici 3 matice jsou tedy stejne:
A(p,:)
ans =
     1     7    17     3
     0     4    10     1
    10    18    40    17
     4     8    18     7
P*A
ans =
     1     7    17     3
     0     4    10     1
    10    18    40    17
     4     8    18     7
% a jeji LU-rozklad:
L*U
ans =
     1     7    17     3
     0     4    10     1
    10    18    40    17
     4     8    18     7
diary off
