Professional Documents
Culture Documents
Column vector: Type the left square bracket [ and then enter
the elements with a semicolon between them, or press
the ENTER key after each element. Type the right square
bracket ] after the last element.
col = [1;2;3;4]
Creating a vector with constant
spacing
Syntax: var_name = [m:q:n]
or var_name = m:q:n
where m is the first term
q is the spacing
n is the last term
Examples:
>> x = [1:2:13]
>> y = [1.5:-0.1:1]
>> z = [-3:7]
Creating a vector with constant
spacing given the number of terms
Syntax: var_name = linspace(xi,xf,n)
where xi is the first element
xf is the last element
n is the number of elements
Examples:
>> va = linspace(0,8,6)
>> vb = linspace(30,10,11)
CREATING A TWO-DIMENSIONAL
ARRAY
• Also called a matrix, has numbers in rows
and columns
• Size of a matrix: mxn
where m is the number of rows
n is the number of columns
• Created by assigning the elements of a
matrix to a variable
Syntax: var_name = [ 1st row elements; 2nd
row elements; 3rd row elements; …;last row
elements]
Examples:
>> a = [5 53 43; 4 7 78; 35 67, 8]
>> b = [ 2 4 6 7 8
3 4 67 2 9
23 4 5 6 34]
Examples:
>> cd = 6; e = 3; h = 4;
>> Mat = [e, cd*h, cos(pi/3);h^2,
sqrt(h*h/cd), 14]
Rows of a matrix can also be entered as
vectors using the notation for creating
vectors with constant spacing or the
linspace command.
Example:
>> A = [1:2:11;0:5:25;linspace(10,60,6);
67 2 43 68 4 13]
REVIEW
The MATRIX in MATLAB
Columns (n) A(2, 4)
1 2 3 4 5
16
A= 1 45
1
3.4
6
-8
11
3.23 1
21
A(17)
2 7 12 17 22
2 56 76 2.34 4 2
Rows 3 3 8 13 18 23
(m)
273 21 4.5 2.56 2.06
4 9 14 19 24
4 78 2.12 98 -4.67 2.45
5 10 15 20 25
5 2 23 7 1 0.2
ENTERING NUMERIC ARRAYS
Use square
brackets
Column separator
semi-colon
Row separator
Creatingspace/comma
sequences
using the colon operator
Loading pre-existing
variables using LOAD
ENTERING NUMERIC ARRAYS
Using other
MATLAB
expressions
Matrix element
assignment
Matrices must be
rectangular. Undefined
elements set to zero
The MATRIX in MATLAB
5 -2 1 4 7 6
A= and B =
3 0 -4 -1 8 -3
3 2 0 4 4 1 3 3 0 1 4 5
2 2 2 4 11 2 3 2 1 1 5
2 -29 12 -6 -5
AB =
BA = 14 -2 -15
3 -1
7 10 -9
.‘ comp. transpose
.^ power
.* multiplication
./ division
Scalar Addition
MATLAB allows the addition, subtraction,
multiplication and division of scalars to
matrices of any size (known as scalar
expansion).
>> w = [ 1 2; 3 4 ] + 5
Array Multiplication
When using array multiplication (.*), the
following rules apply:
• Matrices must have the same
dimensions
• Dimensions of resulting matrix are the
dimensions of the multiplied matrices
• Resulting elements are the product of
corresponding elements
Type the following in the command window:
>> a = [ 1 2 3 4; 1 2 3 4 ]
>> b = [ 1: 4; 1:4]
>> c = a.*b
For a 2 x 2 matrix:
>> A [1 3;-1 5]
A=
1 3
-1 5
>> det(A)
ans =
8
Example:
For a 3 x 3 matrix:
x + 8y + z = 10 7 - 4 8
>> Dz = [3 -4 7;4 1 –1;1 8 10]
Dx = - 1 1 - 6
10 8 >> x = det(Dx)/det(D)
1
3 7 8 >> y = det(Dy)/det(D)
3 – 4 7
Dy = 4 – 1 – 6 4 1 - 1
1 10 1 Dz =
>> z = det(Dz)/det(D)
1 8 10
Systems of Linear Equations
1. 4 x 4 all zeros-matrix
2. 3 x 4 all ones-matrix
3. 5 x 4 all five-matrix
4. 4 x 4 magic square
To create a 4 x 4 magic square:
16 2 3 13
17 11 10 8
9 7 6 12
4 14 15 1
Transpose of a Matrix
The transpose of a matrix is a new matrix in
which the rows of the original matrix are the
columns of the new matrix.
5 2 3 7 1 8 A(4:5,2:3)
A([9 14;10 15])
ARRAY ADDRESSING/INDEXING
Array indices are the row and column
numbers of an element and they are used to
keep track of the array’s elements.
Illustration:
Address Description
A(2) refers to 2nd element in the vector v
A(3,4) refers to the element in row 3,
column 4
A(:) represents all the row or column
elements of the vector v
A(1:3) represents the 1st through 3rd
elements of v
A(:, 2) denotes all the elements in the 2nd
column of A
A(:, 1:4) denotes all the elements in the 1st
through 4th column of A
A(3, :) denotes all the elements in the 3rd
row of A
A(2:4, :) denotes all the elements in the 2nd
through 4th row of A
A(2:3,1:3) denotes all the elements in the 2nd
through 3rd row that are also in the
1st
Example of Programming:
>> A=rand(4,4) % creates a 4 x 4 matrix A
>> col1=A(:,1) % stores the elements of
the 1st column of A to col1
>> row2=A(2,:) % stores the elements of the 2nd
row of A to row2
>> [sorted,i] = sort (col1)
% sorts the elements of col1
from lowest to highest
>> sorted = col1(i)
% arranges the elements from
highest to lowest
Utilize helpwin
elmat.