W6Q1: Replicate an array Description ---------------------------------------------------------- *Identify the function by which you can replicate or* _tile_ *an array of numbers, and then use it to beat Andy Warhol at his own game. You are given a _specific_ image, and asked to tile it, i.e., repeat it, into a canvas that is 11 times the width of the original, and 17 times the original height. Save the result in a vector* |Y|. *Note that we are not being very explicit here! You'll have to find where the image is on your own. Learn to use* |whos|! *If you don't know what the function is that performs the required tiling, learn to use* |lookfor|! Solution Template ---------------------------------------------------- % Don't change this line, but look at what it does! load('clown'); % Don't change, but use this line! m=17; n=11; % Now you have an image. Find it! Tile it! Assign the output Y= Reference Solution --------------------------------------------------- % Don't change this line load(clown); % Don't change, but use this line! m=17; n=11; % Now you have an image. Find it! Tile it! Assign the output Y=repmat(X,m,n); Visible Tests -------------------------------------------------------- Hidden Tests --------------------------------------------------------- %% run('solution') assert(isequal(Y,repmat(X,m,n))); W6Q2: Find an occurrence Description ---------------------------------------------------------- *A very useful function,* |find|. *Let's put it to good use.* *You are given a character array, a variable* |a|, *containing a name. Replace all occurrences of the letter O in the original string by the character string representing the number that is the sum of the indices at which the letter O occurred in the original string. * Make your solution as short and sweet as possible!* Solution Template ---------------------------------------------------- % Here is the given name a='MALOOF'; % Use "find" to find the positions of all O's v=find % In a, replace all O's with the requested symbols, keep the variable % name, make it short! a Reference Solution --------------------------------------------------- a='MALOOF'; v=find(a=='O'); a(v)=sprintf('%i',sum(v)); Visible Tests -------------------------------------------------------- Hidden Tests --------------------------------------------------------- %% run('solution'); assert(isequal(a,'MAL99F')) W6Q3: Make a difference Description ---------------------------------------------------------- *The functions* |max| *and* |diff| *can operate along a _dimension_ of choice, i.e. along a _row_, the _first_, or _column_, the _second_ dimension*. *You are given a certain matrix*, |X|. *Find the _lowest column number_ and, for that column, the corresponding _row_ number at which the largest _absolute_ jump occurs in this matrix* |X|, *measured along the _row_ dimension, as well as the _value_ of that jump. Assign your results to the variables* |cr|, |rr|, *and* |jr|. *Next, find the _lowest row number_ and, for that row, the _corresponding column_ number at which the largest _absolute_ jump occurs in this matrix* |X|, *measured across the _column_ dimension, as well as the _value_ of that jump. Assign those results to the variables* |rc|, |cc|, *and* |jc|. Solution Template ---------------------------------------------------- % This produces the matrix X in your workspace load clown % Now determine the required values jr, cr, and rr, in that order [jr,cr]=max(max(abs(diff(X,[],?)),[],?)); % Check that the next time you determine jr, you get the same! [jr2,rr]=max(abs(diff(X(:,?),[],?))); % These should be zero jr-jr2; % Now determine the values jc, rc, and cc, in that order [jc,rc]=max(max(abs(diff(X,[],?)),[],?)); % Check that the next time you determine jc, you get the same! [jc2,cc]=max(abs(diff(X(?,:),[],?))); % These should be zero jc-jc2; % Verify your answer! These should be zero! abs(X(rr+1,cr )-X(rr,cr))-jr abs(X(rc ,cc+1)-X(rc,cc))-jc Reference Solution --------------------------------------------------- load clown [jr,cr]=max(max(abs(diff(X,[],1)),[],1)); [jr2,rr]=max(abs(diff(X(:,cr),[],1))); [jc,rc]=max(max(abs(diff(X,[],2)),[],2)); [jc2,cc]=max(abs(diff(X(rc,:),[],2))); % Could clearly leave out the dimensions of the second pass % But you could not get them wrong!! jr-jr2 jc-jc2 abs(X(rr+1,cr )-X(rr,cr))-jr abs(X(rc ,cc+1)-X(rc,cc))-jc Visible Tests -------------------------------------------------------- %% run('solution') assert(isequal(jr,jr2)) assert(isequal(jc,jc2)) assert(isequal(abs(X(rr+1,cr)-X(rr,cr)),jr)) assert(isequal(abs(X(rc,cc+1)-X(rc,cc)),jc)) Hidden Tests --------------------------------------------------------- %% run('solution'); assert(isequal(jr,79)) assert(isequal(cr,80)) assert(isequal(rr,1)) assert(isequal(jc,75)) assert(isequal(rc,45)) assert(isequal(cc,101)) W6Q4: Set a colorbar Description ---------------------------------------------------------- *The function* |imagesc| *assigns colors to a matrix of values, and displays the matrix as an image. The colors used derive from a* |colormap|, *and* |imagesc| *makes sure that the lowest and highest values of the matrix take on the extremes of the colors defined in the* |colormap|, *scaling everything else in-between _linearly_. The function* |colorbar| *produces a color legend as a little add-on image, either below or beside the main image*. *You are given such a matrix image, and you are using* |imagesc| *to plot it, and* |colorbar| *to make a color bar for it. You are given a special value*, |v|, *and you are setting the tick marks on the color bar to definitely include it.* *What are the RGB values of the color used to represent that special value? Assign that RGB triad to the vector named* |hh|. *Use* |colormap| *to obtain the* _default_ *colormap (which is the one you get when you ask for nothing in particular) that was used, saving this result in the variable* |h|. *Reassign the color used for the special value in the original colormap to _white_, overwriting the colormap into the variable* |h|. *Return the sum of the absolute values of the new color map as a single number in the variable* |shabs|. *Check that you did the right thing by inspecting your image! You should be rendering the interval containing your special value in _white_, and there should be a tick label right inside of it.* Solution Template ---------------------------------------------------- % Special value, do not change v=61; % Get the image, do not change load mandrill % Display the image, do not change imagesc(X) % Make a colorbar, do not change c=colorbar; % Set the tick marks on the colorbar to include the special value set(c,'Ytick', ) % Get the color map, do not change h=colormap; % What is the color that is being used for the value v? % I recommend first finding that color's row number (i.e. the row of h) colpos= % Now extract the special color from the color map hh= % Set the corresponding color to white, instead, and save the new colormap h % Return the sum of the absolute values of the color map as a single number shabs= % And then check how your picture fares, do not change colormap(h) Reference Solution --------------------------------------------------- v=61; load mandrill; imagesc(X) c=colorbar; minx=min(X(:)); maxx=max(X(:)); mimax=[minx maxx]; set(c,'Ytick',unique([mimax v])) colormap('default'); % FJS test colormap(gray(6)); h=colormap; % Is this an actionable fraction? Must be a completed new interval! colpos=min(1+floor([v-minx]/[maxx-minx]*length(h)),length(h)); % Adam colpos=round((v/c.Limits(2)*length(h))) hh=h(colpos,:); h(colpos,:)=1; shabs=sum(abs(h(:))); colormap(h) Visible Tests -------------------------------------------------------- Hidden Tests --------------------------------------------------------- % These numbers depend on the Cody version!! %% run('solution'); assessVariableEqual('hh',[0.1834 0.5074 0.9798],'AbsoluteTolerance',1e-4) assessVariableEqual('shabs',107.7783,'AbsoluteTolerance',1e-4) W6Q5: Cumulate a distribution Description ---------------------------------------------------------- *A _cumulative distribution function_ is a function that, for a given independent variable* |v|, *returns the probability, under a certain _probability density function_, of all the numbers between negative infinity and the given value* |v|. *For a normal (Gaussian) distribution with an* _expectation of 2_ and* *_a variance of 3,_ *calculate the value of the cumulative *distribution function at the variable* |v|, *and quote your result *as an integer, a rounded percentage, assigned to the variable* *|p|. *You can pick the number* |v| *yourself, as long as it is *positive, which is for my convenience.* *Now you are given a number*, |N|, *and you generate as many samples from the normal distribution with the same expectation and variance. Compute the* |p|*th percentile of your sample, and assign the result to a character string that has a length of precisely three with precisely one digit past the decimal point (which is included in the length!), which you call* |vp|. Solution Template ---------------------------------------------------- % Set the values, do complete with a positive number v= % Set the parameters, do not change, but make sure to use them below ep=1; vr=3; % If you don't understand your answer, make this larger, but not lower N=1e4; % Answer the question, as related to the given distribution p= % Generate N synthetic data, as related to the given distribution x=ep+sqrt(vr)*randn(N,1); % The value of the rounded pth percentile of this data set, as a string vp= Reference Solution --------------------------------------------------- % If they pick really high values, it will still work but we cannot then check that v and vp are the same. That, they need to do. v=1; ep=2; vr=3; N=1e4; p=round(100*normcdf(v,ep,sqrt(vr))); x=ep+sqrt(vr)*randn(N,1); vp=sprintf('%3.1f',prctile(x,p)) Visible Tests -------------------------------------------------------- %% run('solution'); assert(ischar(vp),'I am expecting a character string for vp') assert(isequal(length(vp),3),'I am expecting EXACTLY 3 characters for vp') Hidden Tests --------------------------------------------------------- %% run('solution'); assert(isequal(p,round(100*normcdf(v,ep,sqrt(vr))))) assert(isequal(vp,sprintf('%3.1f',prctile(x,p))))