Search code examples
matlabindexingoctavematrix-indexing

Matlab/ GNU octave syntax


I need to understand a part of the GMRES algorithm written in these languages. What makes me a problem is the following piece:

y = H(1:k,1:k) \ beta(1:k);
x = x + Q(:,1:k)*y; 

Can someone explain what does it mean in extended form.

Thank you in advance.


Solution

  • For what concerns the first equation:

    H(1:k,1:k) = sub-matrix of matrix H that you obtain by taking rows from 1 (beginning) to k and columns from 1 (beginning) to k
    beta(1:k) = sub-vector of vector beta that you obtain by taking elements from 1 (beginning) to k
    y = is a matrix obtained by solving a symbolic matrix left division between sub-matrix of H and the sub-vector of beta
    

    For what concerns the second equation:

    Q(:,1:k) = sub-matrix of matrix Q with all the rows and columns from 1 (beginning) to k
    x = a matrix that is obtained by adding to it's previous value the result of the multiplication between the sub-matrix of matrix Q and y
    

    Indexing in Matlab is 1-based, not 0-based. So index 1 corresponds to the first element of whatever you are working with. Example of sub-matrix by indexing:

    A = [
      2 3 4;
      1 2 3;
      3 4 4
    ];
    
    B = A(1:2,1:2);
    
    B is then equal to:
    
    [
      2 3;
      1 2
    ];
    
    C = A(:,1:2);
    
    C is then equal to:
    
    [
      2 3;
      1 2;
      3 4
    ];
    

    That weird division symbol represents a matrix left division (for more information: mathworks.com/help/symbolic/mldivide.html): X = A\B solves the symbolic system of linear equations in matrix form: A*X = B for X.