Question: 1. Write a MATLAB function called product with one input and one output. The input will be a vector and the output should be the

1. Write a MATLAB function called "product" with one input and one output. The input will be a vector and the output should be the product (or multiplication) of the elements of the input vector. So, for example, if the input is [1 2 3] the output will be 1*2*3 = 6. The input vector could be any length. Although there is an easy way to do this task without using a loop, use a for loop in your function because the point of this problem is to practice using loops.

2. An identity matrix is one where an element is 1 if the row number equals the column number, and 0 otherwise. So, for example, the 2x2 identity matrix is given by [1 0; 0 1], which has a one in row 1, column 1, and has a one in row 2, column 2. The 3x3 identity matrix is given by [1 0 0; 0 1 0; 0 0 1]. Write a MATLAB function called "identity" that has one input and one output. The input will be an integer n and the output will be the nxn identity matrix. Hint: you can generate an nxn matrix of zeros using the command zeros(n). Although there is an easy way to do this task without loops, use loops in your solution because the goal of this problem is to practice using loops.

3. Suppose we flip a coin repeatedly, and we want to count the number of flips that showed heads until the first tail was found. For example, suppose we flipped a coin 10 times, and we got H, H, H, T, H, H, T, T, T, H, where H means heads and T means tails. In this case, the number of heads before the first tail would be 3. Write a MATLAB function called heads with one input and one output, where the input is a string with 'H' for heads and 'T' for tails, and the output is the number of heads until the first tail is found. For convenience, you can assume that the input will have at least one 'T' in it, so that your program can loop through the input elements until a 'T' is found without worrying about going past the end of the input string. For example, if the input is 'HHHTHHTTTH', then the output should be 3. Although there are ways to write this function without a while loop, use a while loop in your function because the point of this problem is to practice using while loops.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!