containing a 1 (true) in each element for which all elements of the
corresponding column of matrix x are nonzero, and a 0 (false) otherwise.
find(x) Returns a vector containing the indices of the nonzero elements of
a vector x. Returns a vector containing
randn(state,s) Resets the state to s.
randn(state,0) Resets the generator to its initial state.
randn(state,J) Resets the generator to its J-th state for integer J.
randn(state,sum(100*clock) Resets the generator to a different state each time.
To generat
m
Histogram value in bin k: N(k), the number of samples with value xmin + (k 1)x
x
xmin + kx. This is also known as the absolute frequency.
The Matlab commands for generating and plotting a histogram are;
143
Command Description
N = hist(x) Returns the r
x
relative frequency
0123456
0
0.02
0.04
0.06
0.08
0.1
0.12
Relative histogram of data2
x
relative frequency
Figure 7.5: Relative frequency histograms
Measures of central tendency
The mean and median describe the middle, or center, of the range of the ran
y(k) =
_k
n=1
x(n) = x(1) x(2) x(k)
If X is a matrix withM rows and N columns with elements denoted x(m, n), m= 1,
2, . . . , M, n =
1, 2, . . . , N, then
The column sum y is the vector having elements y(n), n= 1, 2, . . . , N
y(n) =
M_
m=1
x(m, n) = x(1
5
6
Random Numbers data1
0 50 100 150 200 250 300 350 400 450 500
0
1
2
3
4
5
6
Random Numbers data2
Index, k
Figure 7.1: Random sequences
7.1 Maximum and Minimum
max(x) Returns largest value in vector x, or the row vector of largest elements
of each colu
Many engineering problems require the use of random numbers in the
development of a solution.
In some cases, the random numbers are used to develop a simulation of a complex
problem. The
simulation can be tested over and over to analyze the results, with
truncation of this result. Similarly, the probability of a result in the range [1,2) is 0.5
(50%), which
can be mapped to 1b y truncation. The trunction function in Matlab is floor.
A script to simulate an experiment to flip a coin 50 times and display th
title(Random Numbers - data1),.
subplot(2,1,2),plot(data2),.
title(Random Numbers - data2),.
xlabel(Index, k)
Other data analysis functions available in Matlab are described with the command
help datafun.
135
0 50 100 150 200 250 300 350 400 450 500
0
1
2
xc = [0 1]; % histogram centers
y = hist(coin,xc); % absolute frequency
bar(xc,y), ylabel(frequency),.
xlabel(0: tails, 1: heads),.
title(Absolute Frequency Histogram for 50 Coin Flips)
The output displayed by the script:
coin =
Columns 1 through 12
11001
The product y is the scalar
y=
N_
n=1
x(n) = x(1) x(2) x(N)
The cumulative sum y is the vector having elements y(k), k = 1, 2, . . . , N
y(k) =
_k
n=1
x(n) = x(1) + x(2) + + x(k)
The cumulative product y is the vector having elements y(k), k = 1, 2, .
Hemisphere surface area: Ah = 2R2
Assumptions:
Tank contains no dead air space.
Concrete slab with hermetic seal is provided for the base.
Cost of the base does not change appreciably with tank dimensions.
Computational method:
Express total volume in met
value computed in Section 7.3. The standard deviation can be confirmed by the
value computed
in Section 7.3. The computed values are not identical to the desired values because
the computed
values are due to a finite number of random samples. The computed
statement. The variables modified within the loop should include the variables in
the expression,
or the value of the expression will never change. If the expression is always true (or
is a value that
is nonzero), the loop becomes an infinite loop.
Exampl
More effective filtering functions and better implementations of filtering in Matlab
are available,
but these are beyond the scope of this presentation, which is intended as a short
introduction to
filtering.
174
8.6 Applied Problem Solving: Speech Signal
Control flow
Loops
Selection statements in user-defined functions
Update processes
Applied problem solving: speech signal analysis
8.1 Relational and Logical Operators
For relational and logical expressions:
Inputs: True is any nonzero number
False is 0
> std(data1)
147
ans =
0.5989
> std(data2)
ans =
0.9408
Thus, the variation of data2 is greater than that of data1, as we concluded from
observation of
the plotted data values.
7.4 Random Number Generation
Many engineering problems require the use of rand
500 = R2H +
2
3R3
Solving for H:
H=
500
R2
2R
3
Express cost in dollars as a function of height and radius
C = 300Ac + 400Ah = 300(2RH) + 400(2R2)
138
Method: compute H and then C for a range of values of R, then find the minimum
value of C and
the corre
with the command following the end statement. If expression is a character string,
then a string
comparison is made with the case test expression. Multiple test expressions can be
listed, comma
separated, enclosed in braces cfw_. Only the first matching c
15
20
25
Time (min)
Temperature (degrees C)
Cooling curve
Figure 8.2: Soda cooling curve
Exact Solution
This cooling problem has an exact mathematical solution, which is determined by
expressing the
update relationship as a differential equation and then
-1.0000 -0.6667 -0.3333 0.0000 0.3333 0.6667 1.0000
> sin(x)./x
ans =
0.8415 0.9276 0.9816 1.0000 0.9816 0.9276 0.8415
Logical Operators
Logical operators provide a way to combine or negate relational expressions.
Logical
Operator Description
& and
| or
42 + (1)2 + 32 = 5.1
In Matlab, norm(x) returns the Euclidean norm of row vector or column vector x.
For example, for x and y as defined above:
> nx = norm(x)
nx =
5.0990
> ny = norm(y)
ny =
5.7446
Triangle Inequality
This inequality, also called the Cauc
4. Function M-files terminate execution and return when they reach the end of the
M-file, or
alternatively, when the command return is encountered.
Example 8.8 Step signal
The step signal is zero for negative time, stepping up to one for positive time
u(t
Nested if Statements
if statements may be nested, as shown in the following example:
i f d < 50
count = count + 1;
disp(d);
ifb>d
b = 0;
end
end
Assuming first that b and d are scalars, then if d<50, count is incremented by 1 and
d is displayed.
In additi
Vectors with the same number of elements can be added and subtracted in a very
natural way:
x+y=
x1 +
x2 +
x3 +
.
xn +
y1
y2
y3
yn
,xy=
x1
x2
x3
.
xn
y1
y2
y3
yn
181
Inner or Dot Product
The inner product (x, y), also referred to as the dot product x
The following script is written to do the following:
Represent the ship velocity as a vector S, the current velocity as a vector C, and
the wind-drift
velocity as a vector W. Calculate the true-velocity vector T (velocity over the ocean
bottom).
North re
> A=3; B=[2 4 3]; C=[3 2; 6 1];
> [r c]=indmax(A)
r=
1
c=
1
> [r c]=indmax(B)
r=
1
c=
2
> [r c]=indmax(C)
r=
2
c=
1
166
Avoiding Loops
In general, loops should be avoided in Matlab, as they can significantly increase the
execution time
of a program. Matla
to produce the word. An effective word recognition algorithm must capture this
change in sounds,
but the design of such an algorithm is beyond the material covered in this course.
Note that the
histogram has the appearance of a Gaussian or bell-shaped cur