Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB while if loop 1. What does the find command do? a. Returns the position o

ID: 3762057 • Letter: M

Question

MATLAB while if loop

1. What does the find command do?

a. Returns the position of each nonzero element in an array.'

b. Returns a vector containing the linear indices of each nonzero element in an array.

c. Returns each nonzero element in an array

d. Finds the elements that meet a given criteria and returns those elements directly

2.  Given the following code:

a=0;

while(a<10)

    a = a +3

end

- How many times does the while loop execute?

- What is the final value of a?

3. A midpoint break loop is the best loop to repeat a set of instructions a known number of times whereas a while loop can be used if the number of iterations is unknown. (True or False)

4. If you accidentally create a loop that just keeps running infinitely on a PC and you want to stop it, you should:

a. Make sure the active window is the command window and exit the calculation manually with Ctrl-D

b. Do nothing. Just wait it out!

c. Make sure the active window is the command window and exit the calculation manually with Ctrl-c

d. Make sure the active window is the command window and exit the calculation manually with Ctrl-C

e. Make sure the active window is the editor window and exit the calculation manually with Ctrl-C.

5. Given the following code:

x=input('Enter a value of x greater than 0: ');

if x>0

y=log(x)

else

beep

fprintf('The input to the log function must be positive ')

end %if

If the user then enters [0:0.5:2], does line 3 execute? (True or False)

6. In general loops are best used with:

a. scalars

b. two dimensional matrices

c. row vectors

d. column vectors

e. character arrays

f. cell arrays

7. In general, using a for loop (or a while loop) is less efficient in MATLAB than vectorizing the code. (True or False)

Explanation / Answer

MATLAB while if loop

1. What does the find command do? (B)

a. Returns the position of each nonzero element in an array.'

b. Returns a vector containing the linear indices of each nonzero element in an array.

c. Returns each nonzero element in an array

d. Finds the elements that meet a given criteria and returns those elements directly

2.  Given the following code:

a=0;

while(a<10)

    a = a +3

end

- How many times does the while loop execute? 4 times.

- What is the final value of a? After coming out the loop, the value of a is 12, for which the condition fails.

3. A midpoint break loop is the best loop to repeat a set of instructions a known number of times whereas a while loop can be used if the number of iterations is unknown. (True or False)

4. If you accidentally create a loop that just keeps running infinitely on a PC and you want to stop it, you should: (C)

a. Make sure the active window is the command window and exit the calculation manually with Ctrl-D

b. Do nothing. Just wait it out!

c. Make sure the active window is the command window and exit the calculation manually with Ctrl-c

d. Make sure the active window is the command window and exit the calculation manually with Ctrl-C

e. Make sure the active window is the editor window and exit the calculation manually with Ctrl-C.

5. Given the following code:

x=input('Enter a value of x greater than 0: ');

if x>0

y=log(x)

else

beep

fprintf('The input to the log function must be positive ')

end %if

If the user then enters [0:0.5:2], does line 3 execute? (True or False) No.

6. In general loops are best used with:

a. scalars

b. two dimensional matrices

c. row vectors

d. column vectors

e. character arrays

f. cell arrays

7. In general, using a for loop (or a while loop) is less efficient in MATLAB than vectorizing the code. (True or False)