Saturday, April 21, 2012

Cont. Basic matrix commands and operations in Scilab

7. Matrix inverse
Basically, in Scilab there are two methods to compute the inverse matrix. The first one is using the command "inv(mat)" and the other method is raising the matrix to power equals (-1). Indeed, we all know that the inverse is computed for a square matrix. So.. consider the following square matrix of size (2 x 2)

the first method is like the following

and the second is 
and both methods will return the same result as shown in below

8. Matrix transpose
Matrix transpose is simply rotating each row 90 degrees in clockwise direction about row pivot in order to be column. The transpose matrix is computed simply by adding the quotation mark (') after the matrix like shown below
Note: this note actually is for people who write codes in word documents. If you are writing Scilab codes in MS Word (or even Word Pad) using any font, you should note that the quotation mark in word file differs from the quotation mark typed in Scilab. The latter will return the matrix transpose, but the quotation mark copied from word document will return an error message "Invalid factor". The following figure shows different quotation marks for different fonts.

The quotation mark used in word documents has ASCII number [226    128   152] while that used in Scilab has ASCII number 39.

Computing the transpose matrix for the matrix used in section 7 will give the following answer
9. Matrix multiplication
We can easily multiply two matrices in Scilab (or Matlab) using the asterisk (*). When multiplying two matrices, number of columns of the first matrix must equal number of rows of the second matrix. Consider the following two matrices
to multiply these two matrices we will use the following command

The answer of matrix multiplication is


10 . Element wise matrix operations
Conceptually, in matrix addition and subtraction we add/subtract each element in the first matrix with/from the corresponding element in the second matrix. So, what should we do if we want to multiply/divide each element in the first matrix by the corresponding one in the second matrix?. The answer is element-wise matrix multiplication or division. All what you have to do is adding dot (.) before the multiplication mark (*) or the division mark (/). See the command below for element-wise matrix multiplication

Consider the following two matrices.


element-wise matrix multiplication will return the following answer
Notes:
  •  Element wise matrix multiplication (and the same for division) is commutative which means
  • Element-wise operations require two matrices of the same size

No comments:

Post a Comment