Professional Documents
Culture Documents
Arrays and Matrices
Arrays and Matrices
by
Mohamed Hussein
compliments to
Introduction to Matlab
Array Mathematics
Matrices and Matrix Manipulation
Matrix Operations
Scalar-Array Mathematics
>> a = [1 2 3 4];
>> a - 2
ans =
-1 0 1 2
>> 2*a + 1
ans =
3 5 7 9
Array-Array Mathematics
When arrays have the same length addition,
subtraction, multiplication and division apply
on an element-by-element basis.
>> a = [1 2 3 4];
>> b = [5 6 7 8];
>> c = a + b
c=
6 8 10 12
Array-Array Mathematics (cont.)
Element-by-element multiplication uses dot
multiplication symbol .*
>> a = [1 2 3 4];
>> b = [5 6 7 8];
>> c = a.*b
c=
5 12 21 32
Array-Array Mathematics (cont.)
Element-by-element division uses dot
division symbol ./
>> a = [1 2 3 4];
>> b = [5 6 7 8];
>> c = a./b
c=
0.2 0.3333 0.4286 0.5
Array Orientation
Row vector
>> a = [1 2 3 4]
a=
1 2 3 4
Column vector
>> b = [1; 2; 3; 4]
b=
1
2
3
4
Array Orientation (cont.)
The Matlab transpose operator (‘) changes the
row vector into the column vector
>> a = [1 2 3 4]
a=
1 2 3 4
>> b = a’ or >> b = [1 2 3 4]'
b=
1
2
3
4
Matrices and Matrix Manipulation
An array can be used to represent a vector. For
example, a vector q=3i + 4j + 12k is represented in
MATLAB as [3 4 12] or [3,4,12] . Understand this
concept can help to solve a lot of mathematical
problems
Arrays having multiple rows and columns are called
matrices. A 3x2 matrix is an array with 3 rows and
2 columns. MATLAB displays rows horizontally
and columns vertically
Matrix Manipulation (cont.)
Spaces are used to separate elements in a specific row, and
semicolons are used to separate individual rows:
>> A = [1 -2;3 4] or >> A = [1, -2;3, 4]
A=
1 -2
3 4
>> A(1,3) = 0
A=
1 2 0
4 5 6
Changes the element in the first row and third column to zero.
Matrix Manipulation (cont.)
>> A(4,3) = 1
A=
1 2 0
4 5 6
0 0 0
0 0 1
Places one the in the fourth row and third column. Since A does
not have four rows, the size of a is increased as necessary and
filled with zeros so that the matrix remain rectangular.
Matrix Manipulation (cont.)
>> A =[1 2 3;4 5 6];
>> B = A(2,:)
B=
4 5 6
>> B = A(1:2,1:2)
B=
1 2
4 5
>> B = A(2:-1:1,:)
B=
4 5 6
1 2 3
Creates matrix B by taking the rows of A in reverse order. The final
single colon means “take all columns”.
Matrix Manipulation (cont.)
>> D = [A;B]
D=
1 2 3
4 5 6
4 5 6
1 2 3
Matrix Manipulation (cont.)
>> A = [1 2 3;4 5 6];
>> B = A(:)
B=
1
4
2
5
3
6
builds B by stretching A into a column vector by
taking its columns one at a time.
Matrix Manipulation (cont.)
>> A = [1 2 3;4 5 6];
>> B = A
B=
1 2 3
4 5 6
>> B (:,2) = [ ]
B=
1 3
4 6
>> B (2,:) = [ ]
B=
1 2 3
>> A(2,:) = B
A=
1 2 3
7 8 9
>> B = [abs(A)>1]
B=
0 1 1
0 0 1
>> C = A(B)
C=
–4 –3 –2 2 3 4
>> A = [1 2 3;4 5 6]
A=
1 2 3
4 5 6
>> [x,y]=find(A>4)
x=
2
2
y=
2
3
Here the indices stored in x and y are the row and column indices, where
the relational expression is True. Elements are scanned by columns.
Try:
A=[1 2 3;3 4 5;5 6 7]
Matrix Manipulation (cont.)
>> A = [1 2 3;4 5 6]
A=
1 2 3
4 5 6
>> x = size(A)
x=
2 3
The size function returns a row vector whose
first element is the number of rows and whose
second element is the number of columns.
More on Array and Matrix
find(x) – computes the indices on non
zero elements of the array x
logspace(a,b,n) - creates an array of n
logarithmically spaced between a and b
max(A) - returns algebraically largest
element (if A is an array) or a row array
containing the largest element (if A is a
matrix). Will look for the largest magnitude if
complex elements exist
min(A) - similar to max(A) but returns
minimum values
sort(A) - sorts each column of array A in
ascending order and returns an array the same
size as A
Matrix Scalar Operation
To increase the value of each element of a matrix, direct scalar
addition, subtraction, multiplication or division can be used
>> P = [2 3 8; 5 4 6];
>> Q = 10+P
Q=
12 13 18
15 14 16
>> Q = P-2
Q=
0 1 6
3 2 4
>> Q = P/2
Q=
1.0000 1.5000 4.0000
2.5000 2.0000 3.0000
>> A ./ B
ans =
0.1429 0.2500 0.3333
2.5000 2.2000 2.0000
How about A .\ B?
Matrix Element by Element Operation
(cont.)
40 46 24 33 42
94 106 34 47 60
44 61 78
ASSIGNMENT
Four weekend Programming classes consist of students with the following numbers:
Class I=32 students, Class II=40 students, Class III=34 students and Class IV=28
students. The distribution of students’ average marks for each class for four (4) tests is
given as the followings:
Matrix division uses both right and left division ‘/’ and ‘\’ not ‘./’ and
‘.\’) while matrix exponentiation for example A2 = AA can be
obtained by typing A^2 (not A.^2). However A should be a square
matrix (which has the same number of rows and columns).
>> a = eye(3)
a=
1 0 0
0 1 0
0 0 1
a 3-by-3 identity matrix.