Problem Solving With MATLAB: CPET 190
Problem Solving With MATLAB: CPET 190
CPET 190
Lecture 5
Problem Solving with MATLAB
http://www.etcs.ipfw.edu/~lin
September 15, 2005 Lecture 5 - By Paul Lin 2
Lecture 5: More On MATLAB
Arrays
5.1 More MATLAB Functions
5.2 Array Operators
5.3 Array Arithmetic Operations
5.4 Sub-arrays
September 15, 2005 Lecture 5 - By Paul Lin 3
5.1 More MATLAB Functions
size(this_array)
returns the two values specifying the
number of rows and columns in this_array
length(this_array)
returns the length of a vector, or the
longest dimension of a 2-D array
zeros(n)
built-in MATLAB function for creating an n-
by-n array with all elements are initialized to
zero
zeros(n, m)
for creating an n-by-m all-zero array
September 15, 2005 Lecture 5 - By Paul Lin 4
5.1 More MATLAB Functions
(continue)
ones(n)
built-in MATLAB function for creating an n-
by-n array with all elements are initialized to
one
ones(n, m)
for creating an n-by-m all-one array
eye(n)
for creating an n-by-n identity matrix in
which all elements in the main diagonal are
ones
eye(n, m)
for creating an n-by-m identity matrix
September 15, 2005 Lecture 5 - By Paul Lin 5
5.1 More MATLAB Functions
(continue)
>> help rand
RAND Uniformly distributed random numbers.
RAND(N) is an N-by-N matrix with random entries, chosen
from a uniform distribution on the interval (0.0,1.0).
RAND(M,N) and RAND([M,N]) are M-by-N matrices with
random entries.
RAND(M,N,P,...) or RAND([M,N,P,...]) generate random
arrays.
RAND with no arguments is a scalar whose value
changes each time it is referenced.
RAND(SIZE(A)) is the same size as A.
September 15, 2005 Lecture 5 - By Paul Lin 6
5.1 More MATLAB Functions
(continue)
>> help fix
FIX Round towards zero.
FIX(X) rounds the elements of X to the nearest
integers towards zero.
See also FLOOR, ROUND, CEIL.
September 15, 2005 Lecture 5 - By Paul Lin 7
5.1 More MATLAB Functions
(continue)
Example 5.1: Creating
arrays using array
functions
n = 3; m = 2;
% an 3 x 3 all-zero array
A0 = zeros(3)
% an 3 x 2 all zero array
B0 = zeros(3, 2)
% an 3 x 3 all-one array
A1 = ones(3)
% an 3 x 2 all-zero array
B1 = ones(3,2)
%An identify array
A_eye = eye(n)
A1 =
1 1 1
1 1 1
1 1 1
B1 =
1 1
1 1
1 1
A_eye =
1 0 0
0 1 0
0 0 1
A0 =
0 0 0
0 0 0
0 0 0
B0 =
0 0
0 0
0 0
September 15, 2005 Lecture 5 - By Paul Lin 8
5.2 Array Operators
Transpose operator ( ' )
swap the rows and columns of an
array
Colon operator (: )
one of the most useful operator in
MATLAB. We can use it to create
regularly spaced vectors, subscript
matrices, and specify for iterations
September 15, 2005 Lecture 5 - By Paul Lin 9
5.2 Array Operators
Example 5.2: Vector
Transpose Example
rand() random number
generation function
>> % Generate an array
using rand() function
array_r1 = rand(1, 4)
array_r2 = rand(1, 4)
% Convert row vector to
column vector
array_c1 = array_r1'
array_c2 = array_r2'
array_r1 = 0.2722 0.1988
0.0153 0.7468
array_r2 = 0.4451 0.9318
0.4660 0.4186
array_c1 =
0.2722
0.1988
0.0153
0.7468
array_c2 =
0.4451
0.9318
0.4660
0.4186
September 15, 2005 Lecture 5 - By Paul Lin 10
5.2 Array Operators (continue)
Example 5.3: Using rand(),
fix() functions and array
transpose operator
>> A = rand(1,4) *10
A =
0.5789 3.5287 8.1317 0.0986
>> A = fix(A)
A =
0 3 8 0
>> B = fix(rand(1,4)*10)
B =
1 2 1 6
>> C = [A' B']
C =
0 1
3 2
8 1
0 6
September 15, 2005 Lecture 5 - By Paul Lin 11
5.2 Array Operators (continue)
Example 5.4: Using colon
operator to pick-up selected
rows or columns
A(: , j) extracts j-th column of A
A(i, :) extracts the i-th row of A
C =
0 1
3 2
8 1
0 6
C_1 = C(:,1)
C_2 = C(:, 2)
R_1 = C(1,:)
R_2 = C(2, :)
R_3 = C(3, :)
R_4 = C(4, :)
R_1 =
0 1
R_2 =
3 2
R_3 =
8 1
R_4 =
0 6
C_1 =
0
3
8
0
C_2 =
1
2
1
6
September 15, 2005 Lecture 5 - By Paul Lin 12
5.2 Array Operators (continue)
Example 5.5: This problem solving example uses
the colon operator with integers to generate a
regularly spaced temperature vector from 0 to
100 in degree C. We will also print all data to
show that C to F Temperature Conversion
holding a linear relationship.
Analysis (identified equations and MATLAB
equations, and using plot function to show the
linear relationship)
F = 9/5 C + 32
C = 0:10:100;
F = 9/5 * C + 32;
plot(C, F)
September 15, 2005 Lecture 5 - By Paul Lin 13
5.2 Array Operators (continue)
Example 5.5: Solution
% CtoF_plot.m
% Author: M. Lin
% Date: 9/6/04
% Description:
C = 0:10:100;
F = (9/5)* C + 32;
plot(C, F), grid on
xlabel('Degree C'),
ylabel('Degree F')
title(' C vs F')
0 10 20 30 40 50 60 70 80 90 100
20
40
60
80
100
120
140
160
180
200
220
Degree C
D
e
g
r
e
e
F
C vs F
September 15, 2005 Lecture 5 - By Paul Lin 14
5.2 Array Operators (continue)
Example 5.6:
Reconstruct the sine
60Hz signal and its time
vector as a 2-D array
and save it as
sine60hz.mat file. Verify
result by reloading the
program and plot the
sine wave using colon
operator.
Solution:
f = 60 Hz, T = 1/f
Vm = 10 volts,
dt = 0.001*T,
t = 0:dt: 5*T
e = Vm*sin(2*pi*f*t)
% sine60hz_2d.m
% Author: M. Lin
% Date: 9/6/04
% Description:
f = 60; T = 1/f; Vm = 10;
dt = 0.001*T;
t = 0:dt: 5*T;
e = Vm*sin(2*pi*f*t);
sine60 = [t' e'];
save sine60.mat sine60
clear
load sine60.mat
plot(sine60(:,1),
sine60(:,2))
September 15, 2005 Lecture 5 - By Paul Lin 15
5.2 Array Operators (continue)
Example 5.6: MATLAB Solution
0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09
-10
-8
-6
-4
-2
0
2
4
6
8
10
September 15, 2005 Lecture 5 - By Paul Lin 16
5.3 Array Arithmetic Operations
+ Addition
A + B adds A and B arrays of the same
dimension, unless one is a scalar. A scalar
can be added to a matrix of any dimension.
An Example: both A and B are 2-by-2 array,
A + B is
| (a11 + b11) (a12 + b12) |
| (a21 + b21) (a22 + b22) |
September 15, 2005 Lecture 5 - By Paul Lin 17
5.3 Array Arithmetic Operations
(continue)
- Subtraction.
A - B subtracts B from A. A and B arrays
must have the same dimension, unless one
is a scalar. A scalar can be subtracted from
a matrix of any dimension.
An Example: both A and B are 2-by-2 array,
A - B is
| (a11 - b11) (a12 - b12) |
| (a21 - b21) (a22 - b22) |
September 15, 2005 Lecture 5 - By Paul Lin 18
5.3 Array Arithmetic Operations
(continue)
.* Array Multiplication
A .* B is the element-by-element product of
the arrays A and B. A and B must have the
same dimension, unless one is a scalar. A
scalar can be multiplied to a matrix of any
dimension.
An Example: both A and B are 2-by-2 array,
A .* B is
| (a11 * b11) (a12 * b12) |
| (a21 * b21) (a22 * b22) |
September 15, 2005 Lecture 5 - By Paul Lin 19
5.3 Array Arithmetic Operations
(continue)
./ Array Right Division
A ./B is the element-by-element division of
the arrays A and B. A and B must have the
same dimension, unless one is a scalar. A
scalar can be divided by a matrix of any
dimension.
An Example: both A and B are 2-by-2
array, A ./ B is
| (a11 / b11) (a12 / b12) |
| (a21 / b21) (a22 / b22) |
September 15, 2005 Lecture 5 - By Paul Lin 20
5.3 Array Arithmetic Operations
(continue)
Example 5.7: Element-by-
element array
arithmetic operations
A = fix(rand(2)*10)
B = fix(rand(2)*10)
W = A - B
X = A + B
Y = A .* B
Z = A./ B
>> A = fix(rand(2)*10)
B = fix(rand(2)*10)
W = A - B
X = A + B
Y = A .* B
Z = A./ B
A =
9 6
2 4
B =
8 4
7 0
September 15, 2005 Lecture 5 - By Paul Lin 21
5.3 Array Arithmetic Operations
(continue)
Example 5.7: continue
W = A - B
X = A + B
Y = A .* B
Z = A./ B
A =
9 6
2 4
B =
8 4
7 0
>> A - B
W =
1 2
-5 4
>> A + B
X =
17 10
9 4
>> A .* B
Y =
72 24
14 0
>> A./B
Warning: Divide by zero.
(Type "warning off
MATLAB: divideByZero" to
suppress this warning.)
Z =
1.1250 1.5000
0.2857 Inf
September 15, 2005 Lecture 5 - By Paul Lin 22
5.4 SubArrays
Subarrays can be formed by using colon (: )
operator to select a portion of an array
Example 5.9: Subarray Examples
array4 = [10 20 30; -20 -30 -40; 30 40 50]
array5 = array4(1, : ) % [10 20 30]
array6 = array4(: ,1: 2: 3) % [first column third column]
array6 =
10 30
-20 -40
30 50
array1 = [1 2 3 4 5];
array7 = array1(3: end)
array 7 = 3 4 5
array1(end)
ans = 5
September 15, 2005 Lecture 5 - By Paul Lin 23
Summary
5.1 More MATLAB Functions
5.2 Array Operators
5.3 Array Arithmetic Operations
5.4 Subarrays