##### Document Text Contents

Page 9

Tutorial 4 – Sub-matrices

A sub-matrix can be identified by the row and column numbers at which it starts and ends.

Let us first create a matrix of size 5x8.

-->a=rand(5,8)*100 Generates a 5x8 matrix whose elements are generated as

random numbers.

Since the elements are random numbers, each person will get a different matrix. Let us

assume we wish to identify a 2x4 sub-matrix of 'a' demarcated by rows 3 to 4 and columns 2 to

5. This is obtained as a(3:4, 2:5). The range of rows and columns is represented by the range

commands 3:4 and 2:5 respectively. Thus 3:4 defines the range 3, 4 while 2:5 defines the range

2, 3, 4, 5. However, matrix 'a' remains unaffected.

-->b=a(3:4, 2:5) This command copies the sub-matrix of into 'b'.

A sub-matrix can be overwritten just as easily as it can be copied. To make all elements of

the sub-matrix between the above range equal to zero, use the following command:

-->a(3:4, 2:5)=zeros(2,4) This command creates a 2x4 matrix of zeros and puts it into

the sub-matrix of 'a' between rows 3:4 and columns 2:5.

Note that the sub-matrix on the left hand side and the matrix on the right side (a zero matrix

in the above example) must be of the same size.

While using range to demarcate rows and/or columns, it is permitted to leave out the start

(or end) value in the range, in which case it is assumed to be 1 (or the number of the last row or

column). To indicate all rows (or columns) it is enough to use only the colon (:). Thus, the sub-

matrix consisting of all the rows and columns 2 and 3 of a, the command is a(:, 2:3).

Naturally a(:, :) represents the whole matrix, which of course could be represented simply as

a.

Scilab Tutorial Tutorial 4 – Sub-matrices | 5

Page 10

Tutorial 5 – Statistics

Scilab can perform all basic statistical calculations. The data is assumed to be contained in a

matrix and calculations can be performed treating rows (or columns) as the observations and the

columns (or rows) as the parameters. To choose rows as the observations, the indicator is 'r' or

1. To choose columns as the observations, the indicator is 'c' or 2. If no indicator is furnished,

the operation is applied to the entire matrix element by element. The available statistical

functions are sum(), mean(), stdev(), st_deviation(), median().

Let us first generate a matrix of 5 observations on 3 parameters. Let the elements be random

numbers. This is done using the following command:

-->a=rand(5,3) Creates a 5x3 matrix of random numbers .

Assuming rows to be observations and columns to be parameters, the sum, mean and

standard deviation are calculated as follows:

-->s=sum(a, 'r') Sum of columns of a.

-->m=mean(a,1) Mean value of each column of a.

-->sd=stdev(a, 1) Standard deviation of a.

-->sd2=st_deviation(a, 'r') Standard deviation of a. Sample size std.

-->mdn=median(a,'r') Median of columns of a.

The same operations can be performed treating columns as observations by replacing the 'r'

or 1 with 'c' or 2.

When neither 'r' (or 1) nor 'c' (or 2) is supplied, the operations are carried out treating the

entire matrix as a set of observations on a single parameter.

The maximum and minimum values in a column, row or matrix can be obtained with the

max() and min() functions respectively in the same way as the above statistical functions,

except that you must use 'r' or 'c' but not 1 or 2.

Scilab Tutorial Tutorial 5 – Statistics | 6

Tutorial 4 – Sub-matrices

A sub-matrix can be identified by the row and column numbers at which it starts and ends.

Let us first create a matrix of size 5x8.

-->a=rand(5,8)*100 Generates a 5x8 matrix whose elements are generated as

random numbers.

Since the elements are random numbers, each person will get a different matrix. Let us

assume we wish to identify a 2x4 sub-matrix of 'a' demarcated by rows 3 to 4 and columns 2 to

5. This is obtained as a(3:4, 2:5). The range of rows and columns is represented by the range

commands 3:4 and 2:5 respectively. Thus 3:4 defines the range 3, 4 while 2:5 defines the range

2, 3, 4, 5. However, matrix 'a' remains unaffected.

-->b=a(3:4, 2:5) This command copies the sub-matrix of into 'b'.

A sub-matrix can be overwritten just as easily as it can be copied. To make all elements of

the sub-matrix between the above range equal to zero, use the following command:

-->a(3:4, 2:5)=zeros(2,4) This command creates a 2x4 matrix of zeros and puts it into

the sub-matrix of 'a' between rows 3:4 and columns 2:5.

Note that the sub-matrix on the left hand side and the matrix on the right side (a zero matrix

in the above example) must be of the same size.

While using range to demarcate rows and/or columns, it is permitted to leave out the start

(or end) value in the range, in which case it is assumed to be 1 (or the number of the last row or

column). To indicate all rows (or columns) it is enough to use only the colon (:). Thus, the sub-

matrix consisting of all the rows and columns 2 and 3 of a, the command is a(:, 2:3).

Naturally a(:, :) represents the whole matrix, which of course could be represented simply as

a.

Scilab Tutorial Tutorial 4 – Sub-matrices | 5

Page 10

Tutorial 5 – Statistics

Scilab can perform all basic statistical calculations. The data is assumed to be contained in a

matrix and calculations can be performed treating rows (or columns) as the observations and the

columns (or rows) as the parameters. To choose rows as the observations, the indicator is 'r' or

1. To choose columns as the observations, the indicator is 'c' or 2. If no indicator is furnished,

the operation is applied to the entire matrix element by element. The available statistical

functions are sum(), mean(), stdev(), st_deviation(), median().

Let us first generate a matrix of 5 observations on 3 parameters. Let the elements be random

numbers. This is done using the following command:

-->a=rand(5,3) Creates a 5x3 matrix of random numbers .

Assuming rows to be observations and columns to be parameters, the sum, mean and

standard deviation are calculated as follows:

-->s=sum(a, 'r') Sum of columns of a.

-->m=mean(a,1) Mean value of each column of a.

-->sd=stdev(a, 1) Standard deviation of a.

-->sd2=st_deviation(a, 'r') Standard deviation of a. Sample size std.

-->mdn=median(a,'r') Median of columns of a.

The same operations can be performed treating columns as observations by replacing the 'r'

or 1 with 'c' or 2.

When neither 'r' (or 1) nor 'c' (or 2) is supplied, the operations are carried out treating the

entire matrix as a set of observations on a single parameter.

The maximum and minimum values in a column, row or matrix can be obtained with the

max() and min() functions respectively in the same way as the above statistical functions,

except that you must use 'r' or 'c' but not 1 or 2.

Scilab Tutorial Tutorial 5 – Statistics | 6