Starting from:

$29.99

Assignment 2 Vector? I hardly know her!

MATLAB Assignment 2
, Section A
This problem set will cement your understanding of vector operations and go over several important
built-in functions. It will also provide an example of the efficiency to be gained by pre-allocating
your data structures.
As with all the homeworks, please submit it as a .m file, with suppressed output. Remember
that all lectures and homeworks may be found at github.com/guybaryosef/ECE210-materials. This
homework is due by 4:00 PM on February 5th to guybymatlab@gmail.com. Remember to also
bring a hardcopy in to class!
1. Vector? I hardly know her! Here we will look at some applications of built-in vectorized
functions. Label your variables appropriately.
(a) Create a vector of 100 evenly spaced samples of the exponential function over the interval [0,1].
(b) Approximate the integral using the trapezoidal method (use trapz and multiply by the interval)
as well as the rectangular method (sum over all points and multiply by the interval).
(c) Approximate the cumulative integral using the trapezoidal method (use cumtrapz ) and the
rectangular method (use cumsum). Looking at the pair of cumulative values, did you get the
same answers as in part (b)?
(d) Approximate the derivative by taking the difference between all adjacent elements and dividing
by the time spacing. Similarly, approximate the second derivative. What are the lengths of
each derivative vector?
2. Array Foray Perform the following matrix operations.
(a) Use reshape to create a 10 × 10 matrix A where A =





1 11 ... 91
2 12 ... 92
.
.
.
.
.
.
.
.
.
.
.
.
10 20 ... 100





.
(b) Use magic to create a 10 × 10 magic matrix B. Use B to create a matrix C which has the
same diagonal values of B and is zero elsewhere. Note: You might want to look up diag to
see how to do this elegantly.
(c) Flip the second column of B such that it is inverted upside down.
(d) Flip the matrix A from left to right.
(e) Find the column-wise sum of every column of AB (normal matrix multiplication). The result
should be a row vector.
1
(f) Find the row-wise mean of every row of AB (element-wise matrix multiplication). The result
should be a column vector.
(g) Delete the last column of A.
3. Gotta Go Fast Generate a 300 × 500 matrix with entries ai,j =
i
2+j
2
i+j+3 using the following
methods and use tic toc to time the speed of each. Report the times in a table (using the table
function).
(a) Using for loops and no pre-allocation.
(b) Using for loops and pre-allocating memory with zeros.
(c) Using only element-wise matrix operations. Note: repmat and/or meshgrid will be useful
here.
2

More products