Write some basic matrices functions.

Linear Algebra and the C Language · Wikibooks

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From Wikibooks, under CC BY-SA 4.0, as the source states it. This page at Wikibooks.

    Write some basic matrices functions.


    The code for certain matrix functions

    Diagonal matrix

    Copy/Paste into the octave window

    %% Diagonal matrix
    %%
    %% Clear all user-defined variables
    clear
    %% ----------------------- */
    %% ----------------------- */
    function M = diagD(M)
    
    r = size (M, 1);
    
    %% Diagonal matrix
      for i=1:r
    
        M(i,i) = round(10*randn());
      end
    
    endfunction
    %% ----------------------- */
    %% ----------------------- */
    %%
    %% === main ===
    %% Clear the terminal
    clc
    
    rc = 6;
    A  = zeros(rc);
    
    %% Display A
    A = diagD(A)
    
    %% Display A as an indexed color image
    figure(1), clf
    imagesc(A)
    axis square,  title('A')
    
    %% === return(0) ===
    

    Lower triangular matrix

    Copy/Paste into the octave window
    
    %% Lower triangular matrix
    %%
    %% Clear all user-defined variables
    clear
    %% ----------------------- */
    %% ----------------------- */
    function M = trigL(M)
    
    [r,c] = size (M);
    
    %% Lower triangular matrix
    for i=1:r
        for j=1:c
    
            if((i>=j))
    
               M(i,j) = round(10*randn());
            endif
        end
    end
    
    endfunction
    %% ----------------------- */
    %% ----------------------- */
    %%
    %% === main ===
    %% Clear the terminal
    clc
    
    r = 6;
    c = 6;
    
    A  = zeros(r,c);
    
    %% Display A
    A = trigL(A)
    
    %% Display A as an indexed color image
    figure(1), clf
    imagesc(A)
    axis square,  title('A')
    
    %% === return(0) ===
    

    Upper triangular matrix

     Copy/Paste into the octave window
    
    %% Upper triangular matrix
    %%
    %% Clear all user-defined variables
    clear
    %% ----------------------- */
    %% ----------------------- */
    function M = trigU(M)
    
    [r,c] = size (M);
    
    %% Upper triangular matrix
    for i=1:r
        for j=1:c
    
            if((i<=j))
    
               M(i,j) = round(10*randn());
            endif
        end
    end
    
    endfunction
    %% ----------------------- */
    %% ----------------------- */
    %%
    %% === main ===
    %% Clear the terminal
    clc
    
    r = 6;
    c = 6;
    
    A  = zeros(r,c);
    
    %% Display A
    A = trigU(A)
    
    %% Display A as an indexed color image
    figure(1), clf
    imagesc(A)
    axis square,  title('A')
    
    %% === return(0) ===
    

    Symmetric matrix

    Copy/Paste into the octave window

    %% Symmetric matrix
    %%
    %% Clear all user-defined variables
    clear
    %% ----------------------- */
    %% ----------------------- */
    function M = symmetric(M)
    
    [r,c] = size (M);
    
    %% Symmetric matrix
    for i=1:r
        for j=1:c
    
               x = round(10*randn());
    
               M(i,j) = x;
               M(j,i) = x;
        end
    end
    
    endfunction
    %% ----------------------- */
    %% ----------------------- */
    %%
    %% === main ===
    %% Clear the terminal
    clc
    
    r = 6;
    c = 6;
    
    A  = zeros(r,c);
    
    %% Display A
    A = symmetric(A)
    
    %% Display A as an indexed color image
    figure(1), clf
    imagesc(A)
    axis square,  title('A')
    
    %% === return(0) ===
    

    Skewsymmetric matrix

    Copy/Paste into the octave window

    %% Skewsymmetric matrix
    %%
    %% Clear all user-defined variables
    clear
    %% ----------------------- */
    %% ----------------------- */
    function M = skewsymmetric(M)
    
    [r,c] = size (M);
    
    %% Skewsymmetric matrix
    for i=1:r
        for j=1:c
    
              if (i==j)
    
                   M(i,j) = 0;
               else
                   x = round(10*randn());
    
                   M(i,j) =  x;
                   M(j,i) = -x;
               endif
        end
    end
    
    endfunction
    %% ----------------------- */
    %% ----------------------- */
    %%
    %% === main ===
    %% Clear the terminal
    clc
    
    r = 6;
    c = 6;
    
    A  = zeros(r,c);
    
    %% Display A
    A = skewsymmetric(A)
    
    %% Display A as an indexed color image
    figure(1), clf
    imagesc(A)
    axis square,  title('A')
    
    %% === return(0) ===