CS 1112 Exercise 7

Due date: Sunday, April 4, 11:00pm EST.

You have until Sunday, April 4, at 11:00 PM EDT to complete and submit Problems 2 and 3 of this exercise using Matlab Grader. Problem 1 does not require submission, but be sure to do it to learn syntax for accessing subarrays (vectorized code) and 3-d array. Chat with your classmates about your answers.

Subarrays (preview for next lecture)

Type the following expressions in the Matlab Command Window. Write the resulting array or answer the question on each blank.

m= rand(6,5)

a= m(:,2)       % What does the colon specify when used in place of an index? ______________

b= m(2:3,:)     % __________________________________________________________________________

p= rand(6,5,3)  % This is a 3-dimensional array

[nr, nc, np]= size(p)  % ___________________________________________________________________

c= p(:,:,2)     % Is this a matrix (2-d) or a 3-d array? ___________________________________

d= p(4,:,2)     % Is this a vector, matrix, or 3-d array? __________________________________

Find a value in a matrix (answer in Matlab Grader)

Implement the following function as specified. Use loops in this problem; do not use any built-in functions other than size().

function [rvec, cvec] = findInMatrix(n, M)
% Find all occurrences of the number `n` in matrix `M`.
% `rvec` and `cvec` are column vectors of row and column numbers such that
% `M(rvec(k),cvec(k))` is equal to `n`.
% The length of `rvec` and `cvec` is the number of times `n` appears in `M`.
% If `n` is not found in `M`, `rvec` and `cvec` are empty vectors.
% Do not use any built-in functions other than `size()`.

Cumulative sums (answer in Matlab Grader)

Implement the following function as specified. Do not use any built-in functions other than size().

function A = matrixCSums(M)
% `M` is a numeric matrix and `A` has the same size as `M`. Assume `M`
% is not empty. Each element in `A` is the sum of the corresponding
% element in `M` and all the elements above it. Example :
%   M = [ 1 3; ...             A = [ 1  3; ...
%         4 5; ...     then          5  8; ...
%        -7 2]                      -2 10]
% Do not use any built-in functions other than `size()`.