Math 012/012B Numerical Methods and Analysis Matlab Activity 3.1 Matrices
Math 012/012B Numerical Methods and Analysis Matlab Activity 3.1 Matrices
MATRICES
NAME :
SCORE:
The MATLAB environment uses the term matrix to indicate a variable containing real or complex numbers
arranged in a two-dimensional grid. An array is, more generally, a vector, matrix, or higher dimensional grid of
numbers. All arrays in MATLAB are rectangular, in the sense that the component vectors along any dimension are
all the same length.
a =
1 2 3
4 5 6
7 8 9
3. Matlab has different functions that create different kinds of matrices One way to create a matrix is to use
functions, such as ones, zeros, randn, and rand. The ones function will generate a matrix with all elements
equal to 1. The zeros function on the other hand will generate a matrix with all elements equal to 0. The
function rand generates a matrix with uniformly distributed elements while the randn function generates
normally distributed elements.
To generate a matrix of zeros type zeros(a, b) where a are the number of rows and b are the number of
columns or type zeros (x) where x is the order of the matrix for square matrices. The same format is used for
the other commands.
>> ones(3)
ans =
1 1 1
1 1 1
1 1 1
>> randn(1,2)
ans =
0.7172 1.6302
>> rand(1,3)
ans =
0.9649 0.1576 0.9706
4. Another function that will generate matrices are pascal and magic. The pascal function generates symmetric
matrices while the magic function will generate non- symmetric matrices. To create symmetric matrices type
pascal(x) where x is the order of your matrix . Do the same to create non-symmetric matrices but using the
magic function . These functions generate square matrices only.
ans =
1 1 1
1 2 3
1 3 6
>> magic(3)
ans =
8 1 6
3 5 7
4 9 2
5. An nxn identity matrix is denoted by In. MATLAB has a command to generate In with the syntax eye( ).
7. A matrix name can be reused. In such a case the "old" contents are lost.
8. To determine the matrix names that are in use , use the who command.
9. To delete a matrix, use the clear command, followed by a space and then the matrix name.
10. To determine the number of rows and columns in a matrix, use the size command.
Note: Notice that no brackets are displayed and that MATLAB has assigned this matrix the name ans. Every matrix
in MATLAB must have a name. If you do not assign a matrix name, then MATLAB assigns it with ans which is the
default variable name. Name each matrix with different variable names otherwise MATLAB will over write them.
If the matrix is quite large, the screen display will scroll too fast for you to see the matrix.
Display Formats
The format function controls the numeric format of the values displayed. The function affects only how
numbers are displayed, not how MATLAB software computes or saves them.
The different formats are format short, format long, format rat, format bank, format hex. The default format in
MATLAB is short.
x =
1.333333333333333e+00 1.234500000000000e-06
Concatenation
Concatenation is the process of joining arrays to make larger ones. A pair of square brackets [ ] is the concatenation
operator.
1. To concatenate horizontally use commas.
2. To concatenate vertically use semicolons.
Try and Verify!!!
a =
1 3
4 2
b =
1 1
1 2
>> A=[a,b]
A =
1 3 1 1
4 2 1 2
>> B=[a;b]
B =
1 3
4 2
1 1
1 2
Indexing
Every variable in MATLAB is an array that can hold many numbers. When you want to access selected elements of
an array, use indexing.
Another way is by the use of Linear Indexing where a single subscript that traverses down each column, in order, to
the desired element is used.
>> A(6)
ans =
9
To refer to multiple elements of an array, use the colon operator, which allows you to specify a range of the form
start:end.
>> A(1:2,3)
ans =
6
7
>> A(2,:)
ans =
3 5 7
The colon operator also allows you create an equally spaced vector of values using the more general form
start:step:end
>> B= 0: 10:100
>> B=
0 10 20 30 40 50 60 70 80 90 100
NAME :
SCORE:
TASK 1
Write the syntax that will create the following matrices. Use the rational format of MATLAB unless stated
otherwise.
1 1 1 1 1
1.
0 0 0 0 0
1 1 1 1 1
5 0 0
2. 0 5 0
0 0 5
2 2 2
3. 2 2 2
2 2 2
TASK 2
Create a 3x 3 magic matrix A concatenate horizontally with a 3 x 3 pascal matrix B . Write the syntax and
output that will…
2. Extract column 3
1.
2.
3.
4.
Task 3
1. In MATLAB , enter the following matrices
5 i 2 3 4*2 2/3
3
A 0 1 B 1 / 201 5 8.2
4
1 0.0001 (9 4) / 3
4i i
3
2. Display matrix B in long format and matrix A in rational format
3. Compare the new format of A and B to the original format, how do they differ?
2.
3.
MATRIX AND ARRAY OPERATIONS
The dimension /order of a matrix with m rows and n columns is denoted by mxn.
MATRIX OPERATIONS
Addition of Matrices
The sum A+B of two m-by-n matrices A and B is calculated entry wise (A + B)i,j = Ai,j + Bi,j, where 1 ≤ i ≤ m and 1 ≤
j ≤ n.
If A and B are m x n matrices, we write A + (-)B as A – B and call this the difference between A and B .
Scalar Multiplication
The scalar multiplication cA of a matrix A and a number c (also called a scalar) is given by multiplying every
entry of A by c: (cA)i,j = c · Ai,j.
Transpose
The transpose of an m-by-n matrix A is the n-by-m matrix AT (also denoted Atr or AT) formed by turning rows into
columns and vice versa: (AT)i,j = Aj,i.
Multiplication
Is defined only if the number of columns of the left matrix is the same as the number of rows of the right matrix. If A
is an m-by-n matrix and B is an n-by-p matrix, then their matrix product AB is the m-by-p matrix whose entries are
given by the dot-product of the corresponding row of A and the corresponding column of B:
Note:
Array operations work on corresponding elements of arrays with equal dimensions. For vectors, matrices,
and multidimensional arrays, both operands must be the same size. Each element in the first input gets matched up
with a similarly located element from the second input; evaluated element by element.
Matrix Operations in MATLAB
The operations of addition, subtraction, and multiplication of matrices in MATLAB follow the same definitions
If A and B are m x n matrices that have been entered into MATLAB, then
1. The sum in MATLAB is computed by the command A+B
5. In MATLAB, writing AC does not perform an implied multiplication. MATLAB considers AC a new matrix
name, and if AC has not been previously defined, an error will result from entering it.
6. Scalar multiplication 5A denotes scalar multiplication in math language while 5*A is required in MATLAB
7. In MATLAB the transpose operator (or symbol) is the single quotation mark, or prime, '.
Matrix operations follow the rules of linear algebra. By contrast, array operations execute element by element
operations and support multidimensional arrays. The period character (.) distinguishes the array operations from
the matrix operations. However, since the matrix and array operations are the same for addition and subtraction,
the character pairs .+ and .- are unnecessary.
a .* b multiplies each element of a by the respective element of b
a ./ b divides each element of a by the respective element of b
a .\ b divides each element of b by the respective element of a
a .^ b raise each element of a by the respective b element
a.' array transpose (non-conjugated transpose)
>> x ‘* y
ans= 32
>> x .* y
ans = [4 10 18]
TASK 3
1. In MATLAB enter the following matrices with rational format.
1 1/ 2 3
4 5/ 4 9/ 4
A 1/ 3 1/ 4 B 5 2 C D 1
1 2 3
1/ 5 1/ 6 4
Using MATLAB commands compute the following expressions, if possible, using matrix operations.
Given MATLAB Syntax MATLAB Output
1. Product of the transpose of
A and half of D.
1/ 2 3 / 4 1 3i 2 5i
5i 2 2i 4 5/ 4 9/ 4
A 1 1/ 5 B C D 1 i 1
1 4 4i 2 1 2 3
0 2 4 i 2 3i
Using MATLAB commands compute the following expressions, if possible, using array operations.
2. Sum of A, transpose of B,
transpose of the square of
C and twice D.
5. Product of A and B
subtracted from the
product of C and D.