Starting from:

$30

CS113-Homework 4  Solved

Create a single MATLAB script with each problem below as a separate section (hint: %%).  

1.  Create a new script  

2.  Create a comment section at the top with your name, date, HW#, class, etc.  

3.  The first script commands should erase all the workspace data, command window output, and close all figures.  

4.  Create separate sections (%% ) for each problem 5. For problem 1-4, provide an input, call the function and then publish the result.

 

Problem 1 Write a function called even_index that takes a matrix, M, as input argument and returns a matrix that contains only those elements of M that are in even rows and columns.  

 

Problem 2 Write a function called flip_it that has one input argument, a row vector v, and one output argument, a row vector w that is of the same length as v. The vector w contains all the elements of v, but in the exact opposite order. For example, is v is equal to [1 2 3] then w must be equal to [3 2 1]. You are not allowed to use the built-in function flip.

 

Problem 3 Write a function called top_right that takes two inputs: a matrix N and a scalar non-negative integer n, in that order, where each dimension of N is greater than or equal to n. The function returns the n-by-n square subarray of N located at the top right corner of N.

 

Problem 4 Write a function called peri_sum that computes the sum of the elements of an input matrix A that are on the “perimeter” of A. In other words, it adds together the elements that are in the first and last rows and columns. Note that the smallest dimension of A is at least 2, but you do not need to check this. Hint: do not double count any elements!

 



 

Problem 5

  

 

Problem 6 The price, in dollars, of a certain stock over a 10-day period is given in the following array.

Price=[19,18,22,21,25,19,17,21, 27, 29]

Suppose you owned 1000 shares at the start of the 10-day period, and you bought 100 shares every day the price was below $20 and sold 100 shares every day the price was above $25. Write a script to compute (a) the amount you spent in buying shares, (b) the amount you received from the sale of shares, (c) the total number of shares you own after the 10th day, and (d) the net increase in the worth of your portfolio.  

More products