This is the P2PU Archive. If you want the current site, go to www.p2pu.org!
Strength of Scilab, and the primary reason why we are learning how to use it is the fact that it operates on matrices just as easily as an ordinary calculator works with scalar data combined with the fact that all matrix operations are built-in and a host of functions for matrix operations are available. In this session we will learn the following:
Here is how we create a matrix with 2 rows and 3 columns, with the first row containing the numbers 1, 2, 3 and the second row containing the numbers 4, 5, 6:
-->a = [1, 2, 3; 4, 5, 6] // a 23 matrix a = 1 2 3 4 5 6 -->b = [1 2 3; 4 5 6] // Same as above. Note, no commas. Whitespace is same as comma
b =
1 2 3 4 5 6 -->c = [1 2 3 -->4 5 6] // Same as above. Enter instead of semicolon -->d = [1 2 3] // Row vector, matrix of size 1x3 -->x = [1; 2; 3] // Column vector, matrix of size 3x1 -->y = [1 2 3]' // Transpose of a row vector of size 1x3 becomes matrix of size 3x1 -->z = 100 // Scalar, matrix of size 1x1. Check with whos -name z
You can quickly create some frequently required standard matrices of the following types:
-->a = zeros(3,4) // Matrix of size 3x4, with all elements zeros -->b = ones(4, 2) // Matrix of size 4x2, with all element szeros -->c = eye(3, 3) // Identity matrix of size 3x3 -->d = eye(3, 5) // Identity matrix of size 3x5, strange to see a non-square identity matrix -->x = diag([1 2 3]) // Diagonal matrix of size 3x3, diagonal elements given in the row matrix [1 2 3] -->y = diag([1 2 3], 1) // Matrix of size 4x4, matrix [1 2 3] placed one place above the main diagonal -->z = diag([1 2 3], -1) // Same as above, but placed one place below the main diagonal -->m = rand(3, 5) // Matrix of size 3x5, with random numbers as its elements
---
To start with, let us look at the following matrix operations:
-->a = int(rand(3, 3)*10) // Matrix of size 3x3, containing integer random numbers betwwen 0 and 10 -->b = a' // Transpose a, and place it in b -->c = a + b // Add a and b, and place the result in c -->d = a - b // Subtraction -->x = a * b // Multiplication -->b = int(rand(3, 1)*10) // Redfine b for the next operation -->x = a \ b // Solution of linear equations [A] {x} = {b}. // Written as {x} = {b} / {a}. Note, [a] in denominator -->x = a^2 // Same as x = a * a -->y = a^3 // Same as y = a * a * a -->z = sqrt(a) // Matrix with elements which are square roots of corresponding elements of a -->x = abs(a) // Matrix with elements which are absolute values of corresponding elements of a -->y = sin(a) // Matrix with elements which are sine values of corresponding elements of a
---
Some matrix operations, such as addition, subtraction, trigonometric, logarithmic, hyperbolic, sqrt(), abs() etc. are performed elementwise. But others, such as multiplication, exponentiation are not. Sometimes, it may be meaningful to perform these operations elementwise rather as matrix operations. For example, if you want c = a * b to mean an elementwise operation denoted by cij = aij * bij, j=1 to n, i=1 to m, then what you want is elementwise multiplication. In Scilab you can tell Scilab to perform an elementwise multiplication with the operator .*, as follows:
-->c = a .* b // a and b must be of same size -->y = 1 ./ x // Invert each element of x and store it in. yij = 1 / xij
In the second statement above, space between the numerator 1 and decimal point (.) is necessary. 1./x is interpreted as 1.0 / x, which is invalid, unless x is of size 1x1. Similarly, a^2 is a*a, matrix multiplication of a and a. But b = a .* a is an elementwise operation so that bij = aij * aij, that is bij = (aij)^2.
-->a = int(rand(3,4)*10) -->b = a.^2
Can you explain what is a .^3? Is a .^ 0.5 valid? If yes, what does it represent?
The operation a .+ b is an error, because matrix addition is already an elementwise operation.