Computing for Scientists & Engineers

CMPS 1371 (Hawkins)

Exam I: Chapter One to Chapter Seven

Questions are 3 points each, unless otherwise indicated Name ____________________

Show your work!

True / False:

1. A function in Matlab can return multiple variables.

2. If x = [1 2] and y = [3 4], then x * y = [3 8]

3. The ASCII code maps individual characters to their internal numerical representation.

Answer the following:

4. Provide the command to create a vector of the odd whole numbers from 7 to 25.

5. The following commands are executed in MATLAB:

>> a = [-1 8 3 5 7];

>> vec1 = a ( [3 5] );

>> vec2 = find ( a > 5 );

What is the value of the following variables when the code above is executed?

a) vec1 = b) vec2 =

6. Given x = [1 3 -2 1 0 -1 4], provide the command(s) that will:

a) Remove the values of x that are negative

b) Reverse the order of x

Answer the following:

7. Given that x = [1 5 4 2], determine the output of the following commands:

4


a) if x > 3

m = x + 1;

else

m = x – 1;

end

disp(m);

b) m = [ ];

count = 1;

for i = 1:length(x)

if x(i) > 3

m(count) = x(i);

count = count + 1;

end

end

disp(m);

4


8. What is the final value of k at the end of the following code:

4


k = 0;

i = 5;

while i ~ = 0

k = k + i;

i = i – 1;

end

disp(k)

4


9. What is the value of C when the code below is executed?

A = [ 2:3:11 ];

B = ones( 1, 2 );

B = B * 2;

B(4) = 4;

A( end ) = [ ];

C = [ B A ]

Answer the following:

10. What is the value of “a” when this script is executed?

a = 0;

b = [2 1 0 1 2];

for i = b(1:end-1)

a = a + i;

end

11. Given the cell array A = {'cat', [7 6 9], {'dog'}}

Determine the following outputs when the following are entered on the command line:

a) A b) A{2}

12. Read the following piece of code and then answer the questions that follow.

carA = struct('Make', 'Chevrolet', 'Model', 'Impala', 'Year', 1998)

carB = struct('Make', 'Ford', 'Model', 'Taurus', 'Year', 2000)

carC = setfield(carA, 'Year', 1991)

cars = [carA carB carC]

cars(2).Color = 'red'

carA = rmfield(carA, 'Model')

X = cars(2).Year

Y = isfield(carB, 'Model')

What is the output of the following variables when the script above is executed:

a) X = b) Y =

Answer the following:

13. Write a function, called grade, where you accept a vector of test scores. The function will average the scores and then return the letter grade based on the following criteria:

A: average ≥ 90

B: 80 ≤ average < 90

C: 70 ≤ average < 80

D: 60 ≤ average < 70

F: average < 60

Write function:

14. Write a function, called theMin that takes in a vector and returns the minimum value and its position without using the built in min function.

Ex: >> v = [6, 12, 2, 91, -13, 61, 26, 22, 71, 54];

>> [min, position] = theMin(v);

>> min = -13

>> position = 5

Multiple Choice:

15. Consider the following vector in Matlab: vec = [1 0 0 0 0];

Which of the following will produce an error?

a) vec(6) = 1;

b) vec(4) = [ ];

c) vec(0) = 1;

d) vec( [4 5] ) = 1;

16. Which of the following commands will remove the negative values from the Vector A: A = [ 9 -4 2 3 -6 -1],

so that the resulting vector is [9 2 3]

I) A(2) = [ ]

A(5) = [ ]

A(6) = [ ]

II) A( [2 5 6] ) = 0

III) A(6) = [ ]

A(5) = [ ]

A(2) = [ ]

IV) A(A<0) = [ ]

a) I only

b) III and IV

c) I and IV

d) all of the above

17. Which of the following is a valid variable name?

a) 123A

b) A_123

c) case

d) A#123

18. Which of the following is a valid function header?

a) function ret = myFunction(X)

b) function myFunction (X)

c) function = myFunction (X)

d) function myFunction ( )

Multiple Choice:

19. Which one of the following lines will produce an error in MATLAB?

a) 'computer' > 'g'

b) double( 'I hate tests' )

c) double( 'yes!' ) + [1:10]

d) 'donot' + 'cheat'

20. What will be the value of x, when the following line of code is executed?

x = {'hello', [2 4 6], 4.572};

x{1}(2)

a) 'hello'

b) [2 4 6]

c) 4

d) e

21. The following script is executed in matlab:

x = [2 3 4];

y = [1 2 3];

z = x .* y;

disp(['z = ' num2str(z)])

which of the following best describes the output at Matlab’s command line?

a) z = 20

b) z = [2 6 12]

c) z = 2 6 12

d) an error message stating ‘inner matrix dimensions must agree’

22. What is the value of k at the end of the following code fragment?

k = 1;

for i = 1:10

k = k + mod( i, 2 )

end

a) 11

b) 6

c) 5

d) 1

23. If B is any vector, then ~(~B) == B is always

a) Error

b) true

c) false

d) cannot determine

Bonus (5 points)

Write a function fibNumber that accepts a positive integer number and will produce a Fibonacci sequence based on the specified size. fibNumber(6) will return the first 6 Fibonacci numbers.

The first two numbers in the Fibonacci sequence are 1 and 1 and each subsequent number is the sum of the previous two:

Fn = Fn-1 + Fn-2 where F1 = F2 = 1

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …

Example: fibNumber(6)

ans = [1 1 2 3 5 8]


Reference Section:

diag(m) – returns the diagonal elements of the matrix m

factorial(n) – returns n!

find(m) – returns the indices of the true elements of m

getfield(a,att) – returns the value of field att in the structure a – equivalent to a.(att)

image(x) - display the image from the matrix x.

imread(filename) - returns a matrix representation of an image

iscell(a) – checks if a is of class cell (a cell array)

ischar(a) – checks if a is of class char (a string)

isempty(here) – checks if here is null (usually represented by [], the empty vector

isfield(a,att) – checks if att is a field in the structure a

isstruct(a) – checks if a is a structure

length(a) – largest dimension of a

magic(n) – builds a n * n magic square

max(a) – value and index of the max value in a

mesh(x, y, z) – plot the surface defined by the x, y and z arrays with colored lines and white faces

[xx, yy] = meshgrid(x, y) – compute the plaid from the x and y vectors

min(a) – value and index of the min value in a

mod(a, b) – the remainder when a is divided by b

ones(rows, cols) – generate a matrix filled with 1

prod(v) – compute the product of all the elements in a vector v

rmfield(str, att) – removes a structure field

setfield(str, att, value) – set a structure field – equivalent to str.(att) = value

sin(th) – sin of the angle in radians

size(a) – all the dimensions of a

sort(v) – arranges the vector v in ascending numerical order

struct(att,val, … ,att,val) – builds a structure with the given attribute/value pairs

sum(v) – total all the elements in the vector v

surf(x, y, z) – plot the surface defined by the x, y and z arrays with colored faces and black lines

title(str) – titles the plot with the given string

[x, fs] = wavread(filename) – gives the waveform and sampling frequency for a .wav file

(x/y/z)label(str) – labels the plot axes with the given string

zeros(rows, cols) – generate a matrix filled with 0

4