W3Q1: Logically address an array Description ---------------------------------------------------------- *A variable* |m| *denotes the numbers of rows and columns of a matrix* |A| *that a certain MATLAB function* |vander| *builds. For a value* |m| *of your choice, construct a* _column_ *vector* |v| *that has* |logical| *values corresponding to whether individual elements of* |A| *are equal to one, or not. Use the standard MATLAB matrix-to-vector unwrapping whereby matrices are read down the rows first, and then across the columns.* Solution Template ---------------------------------------------------- % You can change the value below m=4; % But you cannot change the line below A=vander(1:m); % And below here goes your final answer v=; Reference Solution --------------------------------------------------- m=6; A=vander(1:m); v=A(:)==1; Visible Tests -------------------------------------------------------- %% run('solution') assert(isequal(size(A,1),m)) assert(isequal(size(A,2),m)) assert(isequal(size(v,1),m)) assert(isequal(size(v,2),1)) Hidden Tests --------------------------------------------------------- %% run('solution'); assert(isequal(size(A,1),m)) assert(isequal(size(A,2),m)) assert(isequal(size(v,1),m)) assert(isequal(size(v,2),1)) assert(isequal(sum(v)-2*m+1)) W3Q2: Address a structure implicitly Description ---------------------------------------------------------- *A* |struct| *contains information on students' names, an alphanumeric individual identification code, and a common group number. Complete the script such that the returned variable* |v| *contains the identification code of* _Bob_. Solution Template ---------------------------------------------------- % Do not change the line below, keep the database intact students=struct('Name',{'Alice' 'Bob'},'ID',{1 'a'},'Group',round(pi)); % Complete the two lines below as per the question l=; q=; % ... but don't change the line below v=students(l).(q); Reference Solution---------------------------------------------------- students=struct('Name',{'Alice' 'Bob'},'ID',{1 'a'},'Group',round(pi)); l=2; q='ID'; v=students(l).(q); Visible Tests -------------------------------------------------------- Hidden Tests --------------------------------------------------------- run('solution') assert(isequal(v,'a')) W3Q3: Learn from your figures Description ---------------------------------------------------------- *A data set and a script to plot it are given. Use the resulting figure itself to determine what the minimum and maximum values of the data were, along each of the axes. Return the sum* |v| *of the four individual values that you now have access to.* Solution Template ---------------------------------------------------- % Do not change the lines below n=2^8; Ari=exp(-i*[0:2*pi/n:2*pi-2*pi/n/2]'*[0:n-1]); plot(real(Ari),imag(Ari),'+'); clear Ari ; axis equal tight % Add to, modify the below to answer the question xl=; yl=; % Minimum and maximum of the independent variable minAr=; maxAr=; % Minimum and maximum of the dependent variable minAi=; maxAi=; Reference Solution---------------------------------------------------- n=2^8; Ari=exp(-i*[0:2*pi/n:2*pi-2*pi/n/2]'*[0:n-1]); plot(real(Ari),imag(Ari),'+'); clear Ari ; axis equal tight xl=xlim; yl=ylim; minAr=xl(1); maxAr=xl(2); minAi=yl(1); maxAi=yl(2); v=minAr+maxAr+minAi+maxAi; Visible Tests -------------------------------------------------------- run('solution') Hidden Tests --------------------------------------------------------- run('solution') assert(isequal(v,0)) W3Q4: Make a formatted string Description ---------------------------------------------------------- *The function* |sprintf| *writes formatted data to a character vector. You will use this function very regularly to annotate figures and the like. Construct three strings that compare* |pi| *to an approximation. Each of them,* |v1|, |v2|, *and* |v3| *contains a* _floating-point_ *format that is* _8 positions long_ *(in total) and quotes* _6 digits after the decimal point_ . *The outputs are suitable as input for, e.g. a* |title| *command.* Solution Template ---------------------------------------------------- % Do not change the lines below notpi=[22/7 355/113]; % Use format '%f' to complete the strings below to each show 6 digits of precision v1=sprintf(' ',notpi(1)); v2=sprintf(' ',notpi(2)); v3=sprintf(' ',pi); Reference Solution---------------------------------------------------- notpi=[22/7 355/113]; v1=sprintf('%8.6f',notpi(1)); v2=sprintf('%8.6f',notpi(2)); v3=sprintf('%8.6f',pi); Visible Tests -------------------------------------------------------- %% run('solution') assert(isequal(length(v1),8),'I am expecting EXACTLY 8 characters for v1') assert(isequal(v1(2),'.'),'I am expecting your decimal point elsewhere for v1') %% assert(isequal(length(v2),8),'I am expecting EXACTLY 8 characters for v2') assert(isequal(v2(2),'.'),'I am expecting your decimal point elsewhere for v2') %% assert(isequal(length(v3),8),'I am expecting EXACTLY 8 characters for v3') assert(isequal(v3(2),'.'),'I am expecting your decimal point elsewhere for v3') % FJS MAYBE REQURING [v1 v2 v3] would send them the right message Hidden Tests --------------------------------------------------------- run('solution') assert(isequal(v1,'3.142857')) assert(isequal(v2,'3.141593')) assert(isequal(v3,'3.141593')) W3Q5: Make an alphanumeric string Description ---------------------------------------------------------- *The function* |sprintf| *does take some getting used to, and you should read up on its various options. Construct a string that combines characters and numbers. You are giving a database with birthdays, and you are pulling from it the information to construct the string that must read exactly 'Frederiks birthday is Aug 5'.* Solution Template ---------------------------------------------------- % Do not change the lines below, keep the database intact bdays=struct('Name',{'Frederik','Adam'},... 'DD',{05 30},'MM',{08 12},'YYYY',{1974 1975}); % Do not change the lines below, I want to see a specific result! nnr=1; bdayn=bdays(nnr).Name; bdayd=bdays(nnr).DD; bdaym=bdays(nnr).MM; % Do not change the line below, but do inspect the result bdayms=datestr(datenum(0,bdaym,1),3); % Now use format descriptors '%s' and '%i', and the variables % bdayn, bdayms and bdayd to complete the line below such that it reads EXACTLY: % 'Frederiks birthday is Aug 5' v=sprintf(' birthday is ', ); Reference Solution---------------------------------------------------- bdays=struct('Name',{'Frederik','Adam'},... 'DD',{05 30},'MM',{08 12},'YYYY',{1974 1975}); nnr=1; bdayn=bdays(nnr).Name; bdayd=bdays(nnr).DD; bdayms=datestr(datenum(0,bdays(nnr).MM,1),3); v=sprintf('%ss birthday is %s %i',bdayn,bdayms,bdayd); Visible Tests -------------------------------------------------------- run('solution') assert(isequal(length(v),27),'I am expecting EXACTLY 27 characters') Hidden Tests --------------------------------------------------------- run('solution') assert(isequal(v,'Frederiks birthday is Aug 5'))