



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




A. Consider the following code :
A = 1:2:20; B = linspace(1,10,10); B((mod(B, 3) == 0)||((mod(B,5)==0)) = []; C = [A(5:end) A(1:5)]; [M in] = max(C); A(in) = -1; D = B([3 2 6]).*2; A(2:6)
What are the values of the variables after the code is run?
B. For each of the following, only a line or two of code is required.
C. Evaluate the following logical expressions as true or false:
A = true B = false
A. Anagrams are words which contain the same letters, except in a different order. For example, “egret” and “greet” are anagrams.
1.) Write a function called isAnagram that consumes two strings and returns a Boolean: true if the strings are anagrams or false if they are not.
Note: you may assume that the strings will all be lowercase letters, with no other characters Hint: Use built-in MATLAB functions.
Examples: A = isAnagram('tears', 'stare') A = 1
B = isAnagram ('good', 'dog') B = 0
2.) Now you decide you want a little more sophistication. For example, “butterfly” and “flutter by” are actually anagrams. Give a string A, write the code to remove all the spaces from A.
B. Consider the following function:
function ret = sillyString(X)
for ind = 1:length(X) if strcmpi(X(ind), 't') ret(ind-1:ind) = 'CS'; elseif X(ind) >= 'A' && X(ind) <= 'Z' ret(ind) = 'z'; else ret(ind) = X(ind); end end
1.) What would be the result of the following lines of code? If the code would produce an error, briefly explain why.
a.) a1 = sillyString('I love CS1371!') a1 = _________________________________________________________
b.) a2 = sillyString('This test is HARD') a2 = _________________________________________________________
A. Your friend keeps all of his expenses for a checking account in a vector called budget. Deposits are positive numbers, and money spent is negative. He writes a piece of MATLAB code to determine his financial situation:
1 if sum(budget) < 10 2 if sum(budget) < 0 3 status = 'in debt'; 4 else 5 status = 'broke'; 6 elseif all(budget > 0) 7 status = 'frugal'; 8 elseif abs(min(budget)) > 500 9 status = 'big spender'; 10 else 11 status = 'fine'; 12 end
i.) budget = [10 200 53]
status = ____________________________________________________
ii.) budget = [-27 1000 -84 -720]
status = ____________________________________________________
iii.) budget = []
status = ____________________________________________________
B. Now, your friend has nice parents willing to help him out. Write a function called parentalAid which takes in one of the statuses from above and returns the amount of money his parents are sending. This should be based on the following:
If status is:
Note: You must use a switch-case statement for this question
A. Consider the following function, which takes in a cell array:
function [out1, out2] = spiffy(ca)
out1 = ca; out2 = {}; for X = 1:length(ca) if ischar(ca{X}) for Y = 1:length(ca{X}) out1{X}(Y) = char(ca{X}(Y)+1); end elseif isnumeric(ca{X}) out2 = [out2 ca(X)]; end end
Now, consider the following cases:
a.) What is the value of A{1}? ____________________________________________________ b.) What is the class of A{1}? ____________________________________________________ c.) What is the value of B? _______________________________________________________ d.) What is the class of B? ________________________________________________________
a.) What is the value of length(C)? _____________________________________________ b.) What is the value of C? _______________________________________________________ c.) What is the value of D? _______________________________________________________
B. Write a function called longV that takes in a cell array and concatenates all the vectors into one long vector. The function outputs the one vector and the average length of the vectors in the cell array. Note: scalars count as a vector of length 1. There may be other items, such as strings, in the cell array.
Example: A = longV({1:3, 'gah', 5, [3 7]}) A = [1 2 3 5 3 7]
Thanks to Jacey Planteen for her help on the test. Last Modified: 3:10PM September 16 th^2007 by Shardul Bhatia