0% found this document useful (0 votes)
24 views

Arrays & Matices

Matlab allows for efficient manipulation of arrays and matrices. Arrays can represent vectors and matrices through numeric elements separated by spaces (within rows) and semicolons (between rows). Basic operations like addition and multiplication can be performed element-wise on arrays or matrices of the same size using operators like + and .* . Matrices support additional operations like matrix multiplication using * when the number of columns of the first matrix equals the number of rows of the second. Special matrices like zeros, ones, and the identity matrix can be generated through functions.

Uploaded by

azlan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Arrays & Matices

Matlab allows for efficient manipulation of arrays and matrices. Arrays can represent vectors and matrices through numeric elements separated by spaces (within rows) and semicolons (between rows). Basic operations like addition and multiplication can be performed element-wise on arrays or matrices of the same size using operators like + and .* . Matrices support additional operations like matrix multiplication using * when the number of columns of the first matrix equals the number of rows of the second. Special matrices like zeros, ones, and the identity matrix can be generated through functions.

Uploaded by

azlan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

INTRODUCTION TO

MATLAB
by
Mohamed Hussein
compliments to
Prof. Michael Negnevitsky
Univerity of Tasmania

Lecture 2
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-be-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-be-element division uses dot


division symbol ./
>> a = [1 2 3 4];
>> b = [5 6 7 8];
>> c = a./b
c=
0.2000 0.3333 0.4286 0.5000

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
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

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

Array element is addressed using a bracket. a(1,2) refers to the


element in first row, column two:
>> A (1,2)
ans =
-2

Matrix Manipulation (cont.)


>> A = [1 2 3;4 5 6]
A=
1
2
3
4
5
6
>> 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.)


>> C = [A B]
C=
1
2
4
5

3
6

>> D = [A;B]
D=
1
2
4
5
4
5
1
2

3
6
6
3

or

>> C = [A, B]

4
1

5
2

6
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
redefines b by throwing away all rows in the second
column of original B. [ ] is the empty matrix.

Matrix Manipulation (cont.)


>> A = [1 2 3;4 5 6];
>> B = A
B=
1
2
3
4
5
6
>> B (2,:) = [ ]
B=
1
2
3
throws out the second row of original B.

Matrix Manipulation (cont.)


>> A = [1 2 3;4 5 6];
>> B = [7 8 9]
B=
7
8
9
>> A(2,:) = B
A=
1
2
3
7
8
9
replaces the second row of A with B.

Matrix Manipulation (cont.)


>> A = [0.1 -2 3;0.9 -0.5 4]
A=
0.1000 2.0000 3.0000
0.9000 0.5000 4.0000
>> B = [abs(A)>1]
A=
0
1
1
0
0
1
creates B by giving ones where the absolute value of
a is greater than 1.

Matrix Manipulation (cont.)


>> A = -4:4
A=
4
3
2
1
0
1
2
3 4
>> B = find(abs(A)>1)
B=
1
2
3
7
8
9
Matlab includes function find that returns the subscripts
where a relational expression is True.
>> 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.

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
sum(A)
- sums the elements in each column of array A
and returns a row vector containing the sums

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
3
2

How about

6
4

Q = 2-P ? Lets try

Matrix Scalar Operation (cont.)


>> P = [2 3 8; 5 4 6];
>> Q = 10*P
Q=
20
30
80
50
40
60
>> Q = P/2
Q=
1.0000
2.5000

1.5000
2.0000

4.0000
3.0000

How about Q = 2/P or Q = P^2 ?


Try Q = P.^2

Matrix Element by Element Operation


MATLAB regards element by element matrix operations as array
operations. For element by element operations, the arrays (matrices)
involved must have the same sizes. The following shows element by
element addition, subtraction, multiplication and division of two arrays.
>> A = [1 2 3; 10
>> A + B
ans =
8
10
14
16
>> A - B
ans =
-6
-6
6
6

11 12]; B=[7 8 9; 4 5 6];

12
18

-6
6

Matrix Element by Element Operation


(cont.)
>> A = [1 2 3; 10 11 12]; B=[7 8 9; 4 5 6];
>> A .*B
ans =
7
16
27
40
55
72
>> A ./ B
ans =
0.1429
2.5000

How about

0.2500
2.2000

0.3333
2.0000

A .\ B?

Matrix Element by Element Operation


(cont.)
>> A = [1 2 3; 10 11 12]; B=[7 8 9; 4 5 6];
>> A .^B
ans =
1
256
19683
10000
161051
2985984

Matrix Operations
Product (multiplication) of two matrices AB is obtained using *
operation (not .*). The number of columns in A must equal to the
number of rows in B. The result of the product is a matrix with the same
number of rows as A and the same number of columns as B i.e if matrix
A has a size of m n and B has a size of n p, the product will be a
matrix of size m p
>> A = [1; 2; 3]; B=[4, 5, 6];
>> A*B
ans =
4 5 6
8 10 12
12 15 18

>> A*B
ans =
32

Matrix Operations

Matrix division (multiplication) 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).

Matrix cross product; A x B and matrix dot product; A.B can be


comupute using cross product and dot product functions which are
cross(A,B) and dot(A,B) respectively.

Special Matrices
>> a = zeros(2)
a=
0
0
0
0
a 2-by-2 matrix of zeros.
>> a = ones(2,3)
a=
1
1
1
1
1
1
a 2-by-3 matrix of ones.

Special Matrices (cont.)


>> a = eye(3)
a=
1
0
0
0
1
0
0
0
1
a 3-by-3 identity matrix.

You might also like