Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

MATLAB beginner's course Bar-Ilan University Computer Center ©

Exercise 2

1) Create the following arrays:


a) V = 2 3 10
b) A = 1 2 3 … 15
c) B = all even numbers between 10 and 20
d) C = 3 3.1 3.2 … 4
e) D = 2 3 10 2 3 10 … (repeat 5 times, overall 15 elements. Use V)
f) E = 5 4 3 … -3 -4 -5
g) F = 1 2 … 10 9 8 … 1
2) Perform the following array operations:
a) List the 5th element of C
b) List all even elements of D
c) Delete all even elements of D
d) List the last element of B
e) delete the first and last elements of B
f) List elements of E in reverse order
g) Replace all odd elements of F with -1
h) Replace all odd elements of A with 1,2,3...
3) Read variables from the file glass.wav .
a) Plot data. What are the values of the x axis?
b) Plot every 10th element (use a new array y).
c) Make new x array corresponding to the array plotted in (b). Plot the new array
versus the new x.
4) Perform the following array calculations:
a) Calculate a, a(n) = 0.1 n n=1:10
2
b) Calculate b, b(n) = n - 0.1 n - 3 n=1:10
c) c = 1/2, 1/3, 1/4, … 1/20
d) d = 1/2, 1/22, 1/42, 1/62, … , 1/222

5) A function f is given by: f (x ) = x2


2x + x + 2
2

a) Create the array x = 0.01, 0.11, 0.21, … , 9.91


b) Calculate y = f(x), and plot y versus x.
c) Save x and y to file function.mat.
6) Running average: For a vector X, a running-average vector R of n points is given by:
n −1
1
Ri =
n

j=0
X i+ j
. R has (n-1) elements less than X.

a) Read variables from guass3.mat, and plot ypeak versus xpeak.


b) compute the running-average vector of 2 points: Rj = (Xj+Xj+1)/2 for both variables.
Plot average ypeak versus average xpeak.
c) Same as (b), with 3 points: Rj = (Xj+Xj+1+Xj+2)/3

Exercise 2 page 1
MATLAB beginner's course Bar-Ilan University Computer Center ©

7) Logical operations:
a) Read variables from glass.wav, and save elements 400 to 420 to a variable d.
b) Calculate an array z in which there are zeros where elements of d are negative and
ones otherwise.
c) Calculate an array s in which there is -1 where elements of d are negative and 1
otherwise.
d) Find out how to calculate the sign of a number, and calculate S, the signs of the
elements of d.
e) Verify by calculation, that s and S are identical.
20
1
8) Find out how to compute the sum of an array, and show that ∑2
n =1
n
≈1

9) Local average: For a vector X, a local-average vector L of n points is given by:

1 n −1
Li = ∑ X n ( i −1) + j . L has elements approximately n times less than X.
n j =0
a) Read variables from guass3.mat, and calculate the number of elements in L. Use
help to find out how to use floor.
b) compute the local-average vector of 2 points: Li = (X2i-1 + X2i)/2 . You can drop up to
2 last elements of X.
c) Same as (b), with 3 points: Li = (X3i-2 + X3i-1 + X3i)/3 .
10) Simpson's integration formula of a discrete function of N points is given by:

h ⎛ N −1 N

F= ⎜ ∑ fi + ∑ fi ⎟ , where h is the spacing of the x values.
2 ⎝ i =1 i=2 ⎠
a) Construct a vector of equally-spaced x values.
b) Choose a simple function (eg. x2, sin(x) etc.) and compute the values fi at the points
xi.
c) Perform the integration according to Simpson's formula. You can use the function
sum.
d) Compute F at the first and last points of x, according to the analytic integral (eg.
x3/3, -cos(x) etc.) and compare the result to what you got in (c).
11) Simple statistics:
a) Read the file glass.wav. Save elements from data: x=elements 2000 to 4000
and y=elements 6000 to 8000.
b) Use the functions sum and length to calculate the means of x and y.
c) Compute the normalized covariance of x and y, which is given by:

∑ (x − x )( y − y )
i i
D= i
, where <x> and <y> are the means of x and y
∑ (x − x ) ( y − y )
2 2
i i
i

respectively. Use the functions sum and sqrt.

Exercise 2 page 2

You might also like