next up previous contents
Next: Sampling a signal Up: Matrices as physiological traces Previous: Matrices as physiological traces   Contents

Matrices in Scilab

We are going to create a matrix of 5 rows and 6 columns composed by random numbers between 0 and 1. To do this, we write at Scilab prompt


-->mat = rand(5,6);

We get the matrix assigned to the variable `mat'. If we had built a matrix composed by many columns or rows, we would not have been interested in seeing it. The semicolon at the end of the previous command tells Scilab not to show the matrix.

If we want to know more about `rand' we could write


-->help rand

It appears a nice window explaining details of the function and some simple but very informative examples.

If we don't remember the accurate name of the function, we could write


-->apropos random

and we would get a list of available functions containing `random' at its definition. (Click on `help' and `demo' button can also aid in improving our knowledge of the program).

We return to `mat' to get the properties of the creature we made. We type


-->type (mat)
 ans  =
 
    1.

`ans' is the answer to our question and indicates that `mat' is a variable of type `1'. If we define a string of characters and ask its type, we get a different one


-->str = 'Scilab'
 str  =
 
 Scilab   

-->type(str)
 ans  =
 
    10.

So, a string of characters is a variable of type `10'. We can learn about different types defined in Scilab with `help type'.

A matrix, a vector and a scalar are variables of the same `type'.


-->scal = 3;              
 
-->vec = [2 3 4 5.3];     
 
-->scalcomplex = 3 + 2* %i;
 
-->type(scal)
 ans  =
 
    1.  
 
-->type(vec)
 ans  =
 
    1.
 
-->type(scalcomplex)
 ans  =
 
    1.

There are a lot of new features in these lines. Some of them, such as the construction of a vector by using brackets or the definition of imaginary values using `%i', are self-evident. I would like to call your attention to the fact that a scalar (real or complex), a vector and a matrix are considered as having the same `type'.

We are often interested in the number of rows or columns of a matrix. To check it, we can use


-->size(mat)
 ans  =
 
!   5.    6. !

Since scalars and vectors belong to the same type as matrices we can equally extract its dimension with the same command


-->size(vec)
 ans  = 
 
!   1.    4. !
 
-->size(scal) 
 ans  = 
 
!   1.    1. !
 
-->size(scalcomplex) 
 ans  = 
 
!   1.    1. !

Sometimes it is useful to get merely the number of rows or columns of a matrix


-->size(mat,1) 
 ans  = 
 
    5.  
 
-->size(mat,2) 
 ans  = 
 
    6.

If we use vectors, the command is slightly different


-->length(vec) 
 ans  = 
 
    4.

Thus, if we apply `length' to a matrix


-->length(mat) 
 ans  = 
 
    30.

we get the product of rows and columns of the matrix.

We created `mat', `str', `vec', `scal' and `scalcomplex' and we can delete them selectively


-->clear str                  // erasing str
 
-->str                        // str does not longer exist
  !--error     4  
undefined variable : str                     
 
 
-->vec                        // checking that vec exists
 vec  = 
 
!   2.    3.    4.    5.3 !

or we can delete all the variables


-->clear                      // clears all variables
 
-->vec                        // checking that vec does not exist 
  !--error     4  
undefined variable : vec

For the moment we have learnt the method of expanding our knowledge about Scilab (help) as well as some methods to define variables, to get their type and size and to delete them. Now perhaps it is convenient to deepen in the relation between curves and matrices.


next up previous contents
Next: Sampling a signal Up: Matrices as physiological traces Previous: Matrices as physiological traces   Contents
je 2006-10-13