3.1 Arrays and Matrices
3.1 Arrays and Matrices
Dr. A. Mile
CUK 1
An array is MATLAB's basic data
structure
• Can have any number of dimensions. Most common are
• vector - one dimension (a single row or column)
• matrix - two or more dimensions
• Scalar - matrices with only one row and one column.
• Arrays can have numbers or letters
2
Creating Matrices
• In MATLAB, a vector is created by assigning the elements of
the vector to a variable.
• This can be done in several ways depending on the source of
the information.
• —Enter an explicit list of elements
• —Load matrices from external data files
• —Using built-in functions
• —Using own functions in M-files
Mat =
3.0000 24.0000 0.5000
16.0000 1.6330 14.0000
>> 2:5
ans =
2 3 4 5 No middle step value - default for the step is 1.
>> -2:3
ans =
-2 -1 0 1 2 3 If the last numbers cannot be obtained by
>> -3:3:10 adding steps to first, then the last element in
ans = the vector will be the number that does not
-3 0 3 6 9 exceed last
>> 1.5:-0.5:-0.5 7
ans = negative step is also possible
1.5000 1.0000 0.5000 0 -0.5000
Create vector with specified number of
terms b/w first and last
v = linspace( xi, xf, n )
• xi is first number
• xf is last number
• n is number of terms (= 100 if omitted)
>>f= linspace(0, 10, 5)
f=
0 2.5000 5.0000 7.5000 10.0000 5 elements, from 0 to 10
>>g= linspace(0, 10)
g=
Columns 1 through 10
0 0.1010 0.2020 0.3030 0.4040 ............................................
......................Columns 91 through 100
9.0909 9.1919 9.2929 9.3939 9.4949 9.5960 9.6970 9.7980 8
9.8990 10.0000
>> va = linspace( 0, 8, 6 ) Six elements
va = 0 1.6000 3.2000 4.8000 6.4000 8.0000
>> va = linspace( 30, 10, 11 )
va=30 28 26 24 22 20 18 16 14 12 10
Decreasing elements
a = 0 0 0 0 d = 1 0 0 0
0 0 0 0
0 1 0 0
0 0 0 0
>> b=ones(4,3) 0 0 1 0
b = 1 1 1 0 0 0 1
1 1 1
>> e=magic(4)
1 1 1
1 1 1 e = 16 2 3 13
>> c = rand(2,3) 5 11 10 8
c =
9 7 6 12
0.8147 0.1270 0.6324 11
diag( ) – diagonal elements of matrix. crate a matrix with elements of a vector,if the
argument is a vector)
>>diag(y)
ans =
1
5
>>diag(x)
ans =
1 0 0 14
0 2 0
0 0 3
Functions to Handle Matrices and Arrays
• size( ) – size (dimensions) of matrix x = [1 2 3];
>>size(y) y= [1 2 3; 4 5 6];
ans =
2 3 (2 rows, 3 columns)
•
det( ) – determinant of a matrix. (matrix must be square to get
determinant)
• >> z= [1 2 3; 4 5 6; 7 8 0];
• >> det (z)
• ans =
• 27
•
• inv( ) – inverse of a square matrix
• >> inv(z)
• ans =
• -1.7778 0.8889 -0.1111
• 1.5556 -0.7778 0.2222 15
• -0.1111 0.2222 -0.1111
Subscripts and Extracting a Sub-Matrix
Can access (read from or write to) elements in
array (vector or matrix) individually or in groups
• Useful for changing subset of elements
• Useful for making new variable from subset of elements
16
Examples
>> A=[35 46 78 23 5 14 81 3 55]
A = 35 46 78 23 5 14 81 3 5
>> A(4)
ans = 23
>> A(6)=27
A = 35 46 78 23 5 27 81 3 5
>> A(2)+A(8)
ans = 49
>> A(5)^A(8)+sqrt(A(7))
17
ans = 134
>> MAT=[3 11 6 5; 4 7 10 2; 13 9 0 8]
Column 1
MAT = 3 11 6 5
Element in 4 7 10 2
row 3 and
column 1 13 9 0 8 Row 3
>> MAT(3,1)
ans = 13
>> MAT(3,1)=20 Assign new value to element in row 3 and column 1
MAT = 3 11 6 5
Only this 4 7 10 2
element
changed 20 9 0 8
>> MAT(2,4)-MAT(1,2)
18
ans = -9
Using a Colon : in Addressing Arrays
The colon : lets you address a range • >> A(3,:) % extract the 3rd row
of elements • ans =
• Vector (row or column) 789
• x(:) - all elements • >> A(:,2) % extract the 2nd
• x(m:n) - elements m through n column
• ans =
• Matrix
• A(:,n) - all rows of column n 2
• A(m,:) - all columns of row m 5
• A(:,m:n) - all rows of columns 8
m through n • >> A([1,3],1:2) % extract 1st and
• A(m:n,:) - all columns of rows 2nd elements of 1st and 3rd rows
m through n • ans =
• A(m:n,p:q) - columns p 1 2
through q of rows 7 8 19
m through n
Adding And Deleting Elements
• Indexing can be used to add and delete
elements from a matrix.
A=
• >> A(5,2) = 5 % assign 5 to the position (5,2); 1 2 3
the uninitialized elements become zeros 4 5 8
7 8 9
• >> A(4,:) = [2, 1, 2]; % assign vector [2, 1, 2] to the 0 0 0
4th row of A 0 5 0
• >> A(5,[1,3]) = [4, 4]; % assign: A(5,1) = 4 and
A(5,3) = 4
• A(4,:) = [ ] - will delete 4th row
• A(:, 3) = [ ] – will delete 3rd column A(2:2:6) = [ ]
ans = 1 7 5 3 6 9
• A(1,2) = [ ] – error ……………. How? 20
• Can’t delete single element in a row or column.
More Examples
v = 4 7 10 13 16 19 22 25 28 31 34
>> u=v([3, 5, 7:10])
u = 10 16 22 25 28 31
22
Examples r1 = [1 2 3 4 ]
r2 = [5 6 7 8 ]
r = [r1,r2] c1 = [1; 2; 3; 4 ]
r = c2 = [5; 6; 7; 8 ]
1 2 3 4 5 6 7 8
c = [c1; c2]
A = 1 2 3
>> Z=[A B] 4 5 6
Z = 1 2 3 7 8 B = 7 8
9 10
4 5 6 9 10
C = 1 0 0
>> Z=[A; C] 0 1 0
Z = 1 2 3 0 0 1
4 5 6
1 0 0 c=
1
0 1 0
2
0 0 1 3
>> Z=[A; B] 4
??? Error using ==> vertcat 5
CAT arguments dimensions are not 23
6
consistent. 7
8
Operations on Matrices
• Previously dealt with scalars (single numbers). Will now
work with arrays, which in general have more than one
number
24
Addition and Subtraction
When adding/ subtracting two arrays A and B, MATLAB
adds/subtracts the corresponding elements (element wise
addition/subtraction)
When add/subtract a scalar to an array, MATLAB adds /
subtracts the scalar to every element of the array
25
Matrix Multiplication
There are two ways of multiplying matrices – matrix
multiplication and elementwise multiplication
MATRIX MULTIPLICATION
• MATLAB denotes this with asterisk (*)
• Number of columns in left matrix must be same as number of rows
in right matrix
26
Matrix multiplication on two vectors
• They must both be the >> h = [ 2 4 6 ]
same size h =
2 4 6
• One must be a row vector >> v = [ -1 0 1 ]'
and the other a column v =
vector -1
0
• If the row vector is on the
1
left, the product is a scalar >> h * v
• If the row vector is on the ans =
right, the product is a 4
>> v * h
square matrix whose side is
ans =
the same size as the vectors -2 -4 -6
0 0 0 27
2 4 6
Array Division
Left division, \:
Left division is one of MATLAB's two kinds of array
division
• Used to solve the matrix equation AX=B
• A is a square matrix, X, B are column vectors
• Solution is X = A-1B
In MATLAB, solve by using left division operator (\), i.e.,
>> X = A \ B
29
Element-by-Element Operations
• Addition and subtraction of arrays is always elementwise
• Multiplication, division, exponentiation of arrays can be
elementwise
• Both arrays must be same dimension
30
Example
ELEMENTWISE MULTIPLICATION
>> A = [1 2; 3 4];
>> B = [0 1/2; 1 -1/2];
>> C = [1 0]’;
>> D = A .* B
D =
0 1
3 -2
>> A .* C
??? Error using ==> times
Matrix dimensions must agree.
31
Built-in Functions for Analyzing Arrays
• mean(v) – mean (average)
• max(v) – maximum value, optionally with index of maximum
• min(v) – minimum value, optionally with index of minimum
• sum(v) – sum
• sort(v) – elements sorted into ascending order
• median(v) – median
• std(v) – standard deviation
• dot(v,w) – dot (inner product); v, w both vectors of same
size but any dimension
• cross(v,w) – cross product; v, w must both have three
elements but any dimension
32
Multi-dimensional Array
•Arrays can have more than two dimensions
•A= [ 1 2; 3 4]
•You can add the 3rd dimension by
•A(:,:,2) = [ 5 6; 7 8]
33