Wednesday, February 11, 2009

Vectors and Matrices - 3

Now, I'll write about specific functions for matrices.

The most common functions that I use:

  • eye(m,n) - identity matrix;
  • zeros(m,n) - matrix of zeros;
  • ones(m,n) - matrix of ones;
  • det(X) - determinant;
  • inv(X) - matrix inverse;
  • pinv(A,[tol]) - pseudoinverse;
  • sum(x,[key]) - sum (row sum, column sum) of vector/matrix entries;
  • prod(x,[key]) - product (row sum, column sum) of vector/matrix entries;
  • mean(x,[key]) - mean (row sum, column sum) of vector/matrix entries;
  • stdev(x,[key]) - standard deviation (row sum, column sum) of vector/matrix entries;
  • geomean(x,[key]) - geometric mean (row sum, column sum) of vector/matrix entries;
  • harmean(x,[key]) - harmonic mean (row sum, column sum) of vector/matrix entries;
  • msd(x,[key]) - mean squared deviation (row sum, column sum) of vector/matrix entries;
  • rand(m1,m2,.. [,key]) - random number generator;
  • grand(m, n, dist_type [,p1,...,pk]) - Random number generator(s);
  • find(X) - find indices of boolean vector or matrix true elements.
Each function has it own purpose.

Let's play, with them.

-->eye(3,3) // identity matrix 3 lines and 3 columns*
ans =

1. 0. 0.
0. 1. 0.
0. 0. 1.

-->rand(5,2) // matrix (5 lines and 2 columns) of random numbers with uniform probability distribution between [0,1]
ans =

0.0683740 0.5442573
0.5608486 0.2320748
0.6623569 0.2312237
0.7263507 0.2164633
0.1985144 0.8833888

-->rand(2,5,'normal') // matrix (2 lines and 5 columns) of random numbers with normal (Gaussian) distribution (mean = 0 and variance = 1)
ans =

1.0478272 - 1.4061926 - 1.7350313 - 0.2143931 2.5891773
- 1.3218008 - 1.0384734 0.5546874 - 2.0735088 0.0424792

-->det(eye(4,4)) // determinant of identity matrix 4 x 4
ans =

1.

-->X = rand(5,5) // X is a matrix (5 x 5) with random values between [0,1]
X =

0.4368588 0.0437334 0.1280058 0.1531217 0.8784126
0.2693125 0.4818509 0.7783129 0.6970851 0.1138360
0.6325745 0.2639556 0.2119030 0.8415518 0.1998338
0.4051954 0.4148104 0.1121355 0.4062025 0.5618661
0.9184708 0.2806498 0.6856896 0.4094825 0.5896177

-->det_X = det(X) // det_X is the determinant of X
det_X =

- 0.0799884

-->[py px] = find(eye(3,3)) // px is the positions (x-coordinates) of numbers nonzeros and py is the positions (y-coordinates) of numbers nonzeros
px =

1. 2. 3.
py =

1. 2. 3.

-->[py px] = find(~eye(3,3)) // the ~ is the logic negative operation, so (~0) = 1 and (~1) = 0**
px =

1. 1. 2. 2. 3. 3.
py =

2. 3. 1. 3. 1. 2.


-->X // the variable X (X = rand(5,5))
X =

0.4368588 0.0437334 0.1280058 0.1531217 0.8784126
0.2693125 0.4818509 0.7783129 0.6970851 0.1138360
0.6325745 0.2639556 0.2119030 0.8415518 0.1998338
0.4051954 0.4148104 0.1121355 0.4062025 0.5618661
0.9184708 0.2806498 0.6856896 0.4094825 0.5896177

-->sum(X,'r') // sum over the columns of X (row of values)
ans =

2.6624119 1.4850001 1.9160468 2.5074436 2.3435661

-->sum(X,'c') // sum over the lines of X (column of values)
ans =

1.6401323
2.3403973
2.1498187
1.9002098
2.8839105

-->sum(X) // sum of all elements of X
ans =

10.914469


So, if anyone has any question, then everyone can do comments.

----------

* If you do a command without send the return to a variable, then the Scilab creates the variable ans (answer) with the return or function or operation.

For example:

-->1+1
ans =

2.

-->cos(%pi)
ans =

- 1.

-->sin(%pi/2)+1
ans =

2.



** I'll write about logic operations in next posts.

No comments: