User:Eml4500.f08.ateam.mcnally/HW1

Matlab Primer, University of Florida

Matrix and Array Operations in MATLAB: Section 3
MATLAB incorporates the following matrix operations: Care must be taken to ensure that sizes of the matrices are compatible or an error message will result, and the computation will not be performed."It is important to observe that these other operations, *, ^, \, and /, can be made to operate entry-wise by preceding them by a period." See the following example for further clarification.
 * Addition +
 * Subtraction –
 * Multiplication *
 * Exponential Power ^
 * Conjugate Transpose ‘
 * Left Division \
 * Right Division /

>>[6,7,8,9].*[6 7 8 9]

ans =

36   49    64    81 Notice that the operation performed was not matrix multiplication but rather an entry by entry multiplication. See the following example for matrix multiplication.

>>A=[1 2;3 4]

A =

1    2     3     4

>> B=[1 2;3 4]

B =

1    2     3     4

>> A*B

ans =

7   10    15    22

Scalar Functions: Section 7
MATLAB has built in functions that operate on scalars, but can also operate on individual elements of a matrix. The most common of these functions are as follows:

See the following examples:

>>sin(3*pi/2)

ans =

-1

>> log(1)

ans =

0

Submatrices and colon notaion: Section 11
"Vectors and submatrices are often used in MATLAB to achieve fairly complex data manipulation effects. "Colon notation" ( which is used both to generate vectors and reference submatrices) and subscripting by integral vectors are keys to effcient manipulation of these objects." A row vector of length n may be defined as 1:n where n represents the length of the vector. The default increment is taken to be 1 unless otherwise specified. See the following examples for further clarification. A row vector of length n may be defined as 1:n where n represents the length of the vector. The default increment is taken to be 1 unless otherwise specified. See the following examples for further clarification.

>>1:5

ans =

1    2     3     4     5

>> 1:2:5

ans =

1    3     5 Both vectors are row vectors from 1 to 5, but the second vector is from 1 to 5 with an increment of 2."

Colon Notation" may also be used to access submatrices of a matrix, and to replace elements within matrices. See the following example for further clarification.

>>A =

1    2     3     4     5     6     7     8     9    10    11    12    13    14    15    16

>> A(1:2,3)

ans =

3    7 This creates a column vector consisting of the first 2 entries of the 3rd column of matrix A.

Comparing efficency of algorithms flops, tic and toc: Section 15
Older versions of MATLAB had the function flops which kept a running total of flops performed to solve and algorithm. In more resent versions of MATLAB the command function flops is unavailable and an error message will appear if the command is entered. The command function “tic, any statement, toc” can be used to obtain the elapsed time (in seconds) it takes MATLAB to solve the algorithm. This enables the user to compare the efficiency of his / her algorithms. See the example below for further explanation.

>> A=[ 1 2 3; 7 8 9; 4 5 6]

A =

1    2     3     7     8     9     4     5     6

>> b= [1 2; 4 5; 8 9]

b =

1    2     4     5     8     9 >> x=A*b

x =

33   39   111   135    72    87 >> tic, x=A*b; toc

Elapsed time is 0.000000 seconds.

Sparse Matrix Computations: Section 19
A great deal on computation time and memory storage space can saved when performing calculations on a matrix that has many zero entries by using the command sparse. “Matlab has two storage modes, full and sparse with full the default. The functions full and sparse convert between the two modes.” "A sparse matrix is stored as a linear array of it’s nonzero elements along with their row and column indices.” See the following example for further explanation.

F =

9    4     0     0     0     0     2     0     7     0     0     0     0     8     1     0     0     0     0     0     4     3     6     0     0     0     0     8     2     4     0     0     0     0     1     4

>> S=sparse(F)

S =

(1,1)       9   (2,1)        2   (1,2)        4   (3,2)        8   (2,3)        7   (3,3)        1   (4,3)        4   (4,4)        3   (5,4)        8   (4,5)        6   (5,5)        2   (6,5)        1   (5,6)        4   (6,6)        4

>> F=full(S)

F =

9    4     0     0     0     0     2     0     7     0     0     0     0     8     1     0     0     0     0     0     4     3     6     0     0     0     0     8     2     4     0     0     0     0     1     4

The command S=sparse(F) creates the sparse matrix S from the matrix F, and the command F=full(S) returns matrix F back to its original form.