Tracing/Conversion
Arrays
Conditionals
Iteration
Cell Arrays
100

Name the two functions convert cell arrays to arrays or double arrays to cell arrays, respectively.

cell2mat & num2cell

100

What happens when you have an NxM array, arr, and do the following operation?

arr(:)

It linearizes the array, turning it into a NxM column vector

100

When is it better to use if statements and when is it better to use switch statements?

if: continuous values (integers)

switch: discrete values (strings, limited integers)

100

Explain when you should use a while loop and when you should use a for loop

While: when you DON'T know how many times you are iterating beforehand

For: when you DO know how many times you are iterating beforehand

100

What is the difference between {} and () when indexing with Cell Arrays?

{}: obtain the actual value WITHIN the cell

(): obtain the entire cell(s) with all the data still contained within their individual cells

200

Given the following direct for loop, turn it into an indirect for loop.

out = [];

for x = vec

   out = [out, mod(x,2)];

end

out = [];

for x = 1:length(vec)

   out(x) = mod(vec(x),2);

end

200

Given a NxM array, arr, output a (N/2)x(M/2) array of the bottom left quadrant. N and M are even numbers.

out = arr(end/2+1:end, 1:end/2);

200

What are the 7 saved words we use for if and switch statements (i.e. key words we use to create these statements)

if, elseif, else, switch, case, otherwise, end

200

What is the code to delete every other element in a vector (i.e. delete every even INDEX) USING ITERATION?

for i = length(vec):-2:1

   vec(i) = [ ];

end

200

Given a Nx1 cell array, col, append that to the end of (N+1)xM cell array, ca, with the header, 'People', to create a new cell array that is (N+1)x(M+1)

col = [{'People'}; col];

ca = [ca, col];

300

Given the following for loop, convert it to a while loop.

vec = [ ];

for i = 1:5

   vec(i) = i^2;

end

vec = [ ];

i = 1;

while i < 6

   vec(i) = i^2;

   i = i + 1

end

300

Given a doubles NxM array, find the sum of all the numbers in the array that are less than 10.

mask = arr < 10;

summ = sum(arr(mask));

300

What is the value of out when the following code is run:

num = 8;

if mod(num,2) == 1 & num == 8

   out = 1;

elseif mod(num,2) == 0 & num < 12

   out = 2;

else

   out = 3;

end

2

300

If we wanted to iterate through each element of a NxM array, arr, what would be the "template" we would want to use?

[r,c] = size(arr);

for i = 1:r

   for j = 1:c

     %Code using arr(i,j)

   end

end

300

Given a NxM cell array, ca, with a header row, how do you isolate JUST THE DATA of the column with the header 'Age'?

headers = ca(1,:);

data = ca(2:end,:);

mask = strcmp(headers, 'Age');

ageCol = data(:, mask);

400

Given the following switch statement, convert it to an if statement.

switch var

   case {1, 3}

     str = 'It is a great number!';

   case 'Joshua'

     str = 'It is a cool person!';

   otherwise

     str = 'I don''t care!';

end

if var == 1 | var == 3

   str = 'It is a great number!';

elseif strcmp(var,'Joshua')

   str = 'It is a cool person!';

else

   str = 'I don''t care!';

end

400

Given a NxM array of doubles, delete the columns with a below average sum.

sums = sum(arr);

avg = mean(sums);

mask = sums >= avg;

arr = arr(:,mask);

400

Given a 1xN vector, vec, of a given datatype, do the following:
   If double, sort it in ascending order.

   If char, sort it in descending order.

   If logical, reverse the vector.

if isnumeric(vec(1))

   vec = sort(vec,'a');

elseif ischar(vec(1))

   vec = sort(vec,'d');

else

   vec = vec(end:-1:1);

end

400

Using iteration, determine the largest number than is divisible by 3 in a given vector, vec. The max function is banned.

maxx = 0;

for x = vec

   if mod(x,3) == 0 & x > maxx

     maxx = x;

   end

end

400

Given a NxM cell array, ca, where the first COLUMN are names and the remaining columns contain test scores, delete all the rows where the average test score of that row is below 80.

data = cell2mat(ca(:,2:end));

avg = mean(data,2);

mask = avg < 80;

ca(mask,:) = [];

500

What does the following code accomplish given an input string, str.

ca = {};

[word, rest] = strtok(str);

while ~isempty(word)

   mask = 'a' >= lower(word) & lower(word) <= 'z';

   word = upper(word(mask));

   ca = [ca; {word}];

   [word, rest] = strtok(rest);

end

It creates a vertical cell array where each cell contains a word (in order) from the string and each word is uppercase & only contains letters.

500

Given an odd number, N, create a NxN array that is 'o' everywhere except on the diagonal, where it will be 'x' (i.e. create a square array that makes a big X)

arr = repelem('o',N,N);

arr(1:N+1:end) = 'x';

arr(N:N-1:end) = 'x';

500

Given a variable, str, write a SWITCH statement that does the following:

   When str is the string 'joshua' (any combination of capital or lowercase letters), output 'Cool'

   When str is the string 'pete' or 'Savannah' (any combination of capital or lowercase letters), output 'Less Cool'

   When str is none of the TA names, output 'irrevelant'

switch lower(str)

   case 'joshua'

     out = 'Cool';

   case {'pete','savannah'}

     out = 'Less Cool';

   otherwise

     out = 'irrevelant';

end

500

What is the code to reverse every individual word in a string, str (string only contains spaces and letters)?

newStr = [ ];

[word, rest] = strtok(str);

while ~isempty(word)

   newStr = [newStr, ' ', word(end:-1:1)];

   [word, rest] = strtok(rest);

end

newStr(1) = [ ];

500

Given a NxM cell array, ca, where the first row is a header column, sort the data in the cell array by the column with the header 'Name', which contain string values, in descending order. Your output should be the entire cell array.

headers = ca(1,:);

data = ca(2:end,:);

mask = strcmp(headers,'Name');

names = ca(:,mask);

[~,inds] = sort(names);

inds = inds(end:-1:1);

data = data(inds,:);

ca = [headers; data];

M
e
n
u