More About Matrices and Arrays

This sections shows you more about working with matrices and arrays, focusing on

 

Linear Algebra
Arrays
Multivariate Data
Indexing in Matrices and Lists

 

 

 

 

 

 

 

Linear Algebra

Informally, the terms matrix and array are often used interchangeably. More precisely, a matrix is a two-dimensional numeric array that represents a linear transformation. The mathematical operations defined on matrices are the subject of linear algebra.

We have discussed about the basics of the matrices earlier itself.  we will take the same example for this section also. Let us use the  magic square matrix,\

        S = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]

Adding  the transpose to a matrix results in symmetric matrix,

        S+S'

produces,

        ans =

        ! 32. 8. 11. 17.   !
        ! 8. 20. 17. 23.   !
        ! 11. 17. 14. 26. !
        ! 17. 23. 26. 2.   !

The multiplication symbol, *, denotes the matrix multiplication involving inner products between rows and columns. Multiplying a matrix by its transpose also produces a symmetric matrix.

        S'*S

This produces,

        ans =

        ! 378. 212. 206. 360. !
        ! 212. 370. 368. 206. !
        ! 206. 368. 370. 212. !
        ! 360. 206. 212. 378. ! 

The determinant of this particular matrix happens to be zero, indicating that the matrix is singular.

        det(S)

        ans =

                0. 

Since the matrix is singular, it does not have an inverse. If you try to compute the inverse with,

        inv(S)

This will produce a warning message,

        warning
        matrix is close to singular or badly scaled.
        results may be inaccurate. rcond = 1.1755E-17

        ans =

        1.0E+14 *

        ! 1.2509999 3.7529997 - 3.7529997 - 1.2509999 !
        ! - 3.7529997 - 11.258999 11.258999 3.7529997 !
        ! 3.7529997 11.258999 - 11.258999 - 3.7529997 !
        ! - 1.2509999 - 3.7529997 3.7529997 1.2509999 ! 

 

 

Arrays

When they are taken away from the world of linear algebra, matrices become two dimensional numeric arrays. Arithmetic operations on arrays are done element-by-element. This means that addition and subtraction are the same for arrays and matrices, but that multiplicative operations are different. SCILAB uses a dot, or decimal point, as part of the notation for multiplicative array operations.

Array operations are useful for building tables. Suppose n is the column vector,

            s =  [1:6]';

Using this column vector we can generate a table of algorithms,

            [s ; log10(s)]

This produces,

            ans =

        ! 1.         0.             !
        ! 2.         .30103     !
        ! 3.         .4771213 !
        ! 4.         .6020600 !
        ! 5.         .69897     !
        ! 6.         .7781513 ! 

 

Multivariate Data

SCILAB uses column-oriented analysis for multivariate statistical data. Each column in a data set represents a variable and each row an observation. The (i,j)th element is the ith observation of the jth variable.

 

For example, consider an data set with two variables,

         m_val =

                ! 100.     82. !
                ! 120.     88. !
                ! 124.     92. !
                ! 97.     76.   !
                ! 110.     80. !

The data contains the blood pressure of patient at various instants of time. Using SCILAB we can do various data analysis for this data set.

For example, if we need the average and deviation

        avg = mean(m_val),dev = st_deviation(m_val)

This produces,

        avg =

        ! 110.2 !
        ! 83.6   !
        dev =

        ! 11.882761 !
        ! 6.3874878 ! 

 

 

Scalar Expansion

Matrices and scalars can be combined in several different ways. For example, a scalar is subtracted from a matrix by subtracting it from each element. 

For example,

            s = ones(4,4); s-1

This produces,

            ans =

        ! 0. 0. 0. 0. !
        ! 0. 0. 0. 0. !
        ! 0. 0. 0. 0. !
        ! 0. 0. 0. 0. ! 

With scalar expansion, SCILAB assigns a specified scalar to all indices in a range. 

For example,

          s(1:2,2:3)=0
            s =

        ! 1. 0. 0. 1. !
        ! 1. 0. 0. 1. !
        ! 1. 1. 1. 1. !
        ! 1. 1. 1. 1. !


            

Matrix Operation

The following Table gives the syntax of the basic matrix operations available in SCILAB

   []  matrix definition, concatenation
   ;     row separator
   ()     extraction m=a(k)
   ()     insertion: a(k)=m
    ’     transpose
   +     addition
   -     subtraction
   *     multiplication
   \     left division
   /     right division
   ^     exponent
   .*     elementwise multiplication
   .\   elementwise left division
   ./     elementwise right division
   .^    elementwise exponent
  .*.  kronecker product
  ./.    kronecker right division
  .\.    kronecker left division


 

The find Function

The find function determines the indices of array elements that meet a given logical condition. In its simplest form, find returns a column vector of indices. Transpose that vector to obtain a row vector of indices.

 For example,

Let us use the find function to generate a random sequence whose elements  1 or -1.

        r_seq  = rand(1,5,'normal');

        r_seq(find(r_seq>=0)) =1;

         r_seq(find(r_seq<0)) =-1

        r_seq =

        ! - 1. - 1. - 1. 1. - 1. !