Homework Solutions - MATLAB
Short Description
Homework solutions for the MATLAB Course offered on Coursera...
Description
Sol ut i onst oHomewor k3 Pr obl em odd_i ndex:
function out function out = odd_index(M) out = M(1:2:end, 1:2:end); end
Pr obl e mi nt _ col :
function v function v = int_col(n) v = [n 1:n-1]'; end
Not et hatt hi si sj us tonepos si bl es ol ut i on.Ther ear eman yo t her s .
Pr obl em r i ch:
function usd function usd = rich(cent) usd = [001 00! 010 02!] " cent'; end
Weus et hef ac tt hatmat r i xmu mul t i pl i c at i ons umsupas etofpr oduc t s .Mul t i pl y i ngar o w v ec t orwi t hac ol umnv ec t orwi l l r es ul ti nas cal ar .Her ei tper f or mst heex ac tc al c ul at i ons wene ed.
Pr obl e ml i ght _ t i me :
function [#ins function [#ins $#] = li%ht_ti#e(#ile) $# = #ile " 1&0; #ins = $# e! &0; end
Pr obl e m pi t t y :
function c function c = *itt+() c = s.rt((:,1) / 2 (:,2) /2); end
Pr obl em pi t t y( al t er nat i v esol ut i on) :
function c function c = *itt+() c = s.rt(su#(' / 2))'; end
He r eweus et h ef a ctt h att hef u nc t i o ns um wo r k sc ol u mnbyc mn ol umn .So ,t r an sp os i ngan d t hens quar i nge v er yel ementwi l l putt hes qua r esoft hec or r es pondi ngasandbsi nt o c o l u mn mn s .Th ef u nc t i o ns u mt h enad dst h em u p,a nds qr tc o mp mp ut e sea c he l e me me nt ' s s quar er oot .Fi nal l y , weneedt ot r ans pos et her es ul tbac ki nt oac ol umnv ec t or .
Pr obl em bot t om_l ef t :
function M function M = otto#_left(,n) M = (end-n1:end, 1:n); 1:n); end
Wen eedt h el a s tnr o wsandt h efi r s tnc ol u mn mn s.Theon l yt r i c kher ei st h atwen ee de nd n +1 ,b ec a us ee nd n : e ndwo ul dge tu sn+1i n de x e san dn otnasr e qu i r e d.
Pr obl em mean_squar es:
function ## function ## = #en_s.ures(nn) ## = #en((1:nn)/2); end
Pr obl em hul k:
function function = hul$(v) = [v' (v')/2 (v')/]; end
Sol ut i onst oHomewor k4 Pr obl em quadr ant s:
function 3 = .udrnts(n) = ones(n); 3 = [ 2" ; " 4"]; end
Pr obl em checker boar d:
function = chec$erord(n,#) = ones(n,#); (1:2:n,2:2:#) = 0; (2:2:n,1:2:#) = 0; end
Pr obl em r andomness :
function r = rndo#ness(li#it,n,#) r = fix(li#it " rnd(n,#)) 1; end
Pr obl em mt abl e:
function [t s] = #tle(n,#) t = (1:n)' " (1:#); s = su#(t(:)); end
I fwemat r i xmul t i pl yac ol umnv e ct orofl engt hNb yar o wv e ct orofl engt hM,eac h el eme ntoft her es ul t i ngNb y M mat r i xwi l l bet hepr oduc tofoneel ementf r om ea ch v ec t or .Ther ef or e,wec anc r eat eamul t i pl i c at i ont abl eb ys et t i ngt hec ol umnv ec t ort o1: N andt her o wv ec t ort o1: M andus i ngmat r i xmul t i pl i c at i on.
Pr obl e mi de nt i t y :
function 5 = identit+(n) 5 = 6eros(n); 5(1 : n1 : n/2) = 1; end
Her ewei nde xi nt oamat r i xwi t hasi ng l ei nde xandMATLABhandl esi ta si fi twasa v ec t o ru si ngc ol umnmaj oror der .Put t i ngonesatt hefi r s tpos i t i onandj ump i ngn+1e v er y t i me,wi l l putt hem ex ac t l yi nt hedi agonal .
So l u t i o nst oHo me wo r k5 Her ear et he" offic i al "s ol ut i ons .Not et hatt her ear emul t i pl eway st os ol v eanynont r i v i al pr obl em, s ot hes ear ej us tr epr es ent at i v ee x amp l es .
Problem generationXYZ function gen = generationXYZ(year) if year < 1966 gen = 'O'; elseif year < 191 gen = 'X'; elseif year < !""" gen = 'Y'; elseif year < !"1# gen = 'Z'; else gen = '$'; en% en%
Problem generationXYZ (alternati&e solution) Us i ngnoi f s t at ement s function gen = generationXYZ(yr) ots = 'O''X''Y''Z''$'*; + ,reate cell array of otions i%- = 1 . sum(yr /= 01966191!"""!"1#); + ,alculate in%e- by comaring year to e%ge &alues gen = otsi%-*; en%
Problem letter2gra%e function 3 = letter2gra%e(score) if score /= 91 3 = '4'; elseif score /= 1 3 = '5'; elseif score /= 1 3 = ','; elseif score /= 61 3 = '7'; else 3 = '8'; en% en%
Problem sort# Us i ngnobui l t i nf unc t i ons function & = sort#(a b c) if a e(-));
ma-%im = ma-(si>e(-)); if min%im == "
+ if one %im == " it must be emty
c = @1 elseif ma-%im == 1 + oterise bot %im == 1 (since ma- == 1) @/ scalar c = "; elseif min%im == 1 + oterise if te smaller %im == 1 @/ &ector c = 1; else c = !; en% en%
Problem classify (alternati&e solution) Us i ngnoi f s t at ement s function y = classify (-) % = si>e(-);
= ro%(%);
+ multilies te to %ims
y = @1 .(/=1) .(/1) .(min(%)/1) + eac a%%e% con%ition increases te anser by one en%
+ Bote tat te Crst to solutions are longer but easier to rea% an% un%erstan% tan tis one
Problem ol%er function a = ol%er(y1m1%1y!m!%!) a = 1; if y1 == y! AA m1 == m! AA %1 == %! a = "; elseif (y1 / y!) ?? (y1 == y! AA m1 / m!) ?? (y1 == y! AA m1 == m! AA %1 / %!)
a = @1; en% en%
Problem ol%er (alternati&e solution) Us i ngnoi f s t at ement s function a = ol%er(y1m1%1y!m!%!) a1 = y1 D #66 . m1 D #1 . %1;
+ %oes not a&e to be e-act %ate in %ays
a! = y! D #66 . m! D #1 . %!;
+ it simly maEes a1 an% a! comarable
a = sign(a! @ a1);
+ sign() returns @1 " or 1 Fust at is nee%e%
en%
+ multilying by #66 or greater is nee%e% because of lea years
Problem mo&ies function can%o = mo&ies(r1min1%urmin1r!min!%urmin!) can%o = false; en%time = r1D6" . min1 . %urmin1;
+ con&ert times to minutes
starttime = r!D6" . min!; if en%time ing tat e %o not nee% to cecE eter te + buLer is emty or not since 0- buLer ill orE eiter ay + Ke tricEy art is o te lengt is an%le% :ile te buLer is + sorter tan !H buLer(1Ien%) is use% Once it reaces !H it turns + into buLer(1Ien%@1) e-actly at is nee%e%
So l u t i o nst oHo me wo r k6
Pr o bl e mn ei g hb or function = neigbor(&) = 0; if min(si>e(&)) == 1
+ must be a &ector
for ii = 1Ilengt(&)@1
+ if lengt is less tan ! loo on't %o anyting
(ii) = abs(&(ii.1) @ &(ii)); en% en% en%
Pr obl em nei ghbor( al t er nat i v es ol ut i on) noe x pl i c i tl oop function = neigbor(&) if lengt(&) < ! ?? min(si>e(&)) = 1 + must be a &ector of at least to elements = 0; else = abs(&(1Ien%@1)@&(!Ien%)); en%
+ taEe te %iLerence of to sub&ectors
+ of lengt (n@1)
en%
Pr o bl e mr e pl a c e_ me bui l dsupt heout putoneel ementatat i me function = relace2me(&abc) if nargin < # b = ";
en% if nargin < G c = b; en% = 0; for E = 1Ilengt(&); if &(E) == a
+ if a is foun%
= 0bc; else
+ e insert b an% c at te en% of te current
+ oterise
= 0&(E); + e insert te original element of & en% en% en%
Pr obl em r epl ac e_me( al t er nat i v es ol ut i on) on l yc han ge st heou t pu tv e c t o rwhe na ni n s t a nc eofai sf o und function = relace2me(&abc) if nargin < # b = "; en% if nargin < G c = b; en% = &;
+ maEe te same as &
i = 1;
+ i is use% to in%e- into
for &i = 1Ilengt(&) if &(&i) == a = 0(1Ii@1) b c (i.1Ien%); i = i . 1;
+ insert b an% c at osition i
+ increment i
en% i = i . 1;
+ i is incremente% in eiter case
en% en%
Pr obl em hal f s um us i ngnes t edl oops function s = alfsum(4) 0ro col = si>e(4); s = "; for ii = 1Iro for FF = iiIcol
+ te column in%e- only starts at te current ro in%e-
s = s . 4(iiFF); en% en% en%
Pr obl em hal f s um ( al t er nat i v es ol ut i on) u si ngas i n gl el o opa nds um function s = alfsum(4) 0nr = si>e(4); s = "; for r = 1Inr
+ for eac ro
s = s . sum(4(rrIen%));
+ sum a%%s u te elements rigt of te %iagonal
(inclusi&e) en%
+ in te current ro
en%
Pr obl em l ar ge_el ement s function foun% = large2element(4)
0ro col = si>e(4); foun% = 0; for ii = 1Iro for FF = 1Icol if 4(iiFF) / ii . FF
+ if te element is larger tan te sum of its in%e-es
foun% = 0foun%; ii FF; + a%% a ne ro to te outut matrien% en% en% en%
p r o bl e mo ne _p er _ n us i ngwhi l el oop function n = one2er2n(-) n = "; sum = "; ile sum < - AA n ero for o%% numbers tat is logical true
en% + note tat tis ill ut non@integers into o%%
Pr obl em di v v y function 4 = %i&&y (4E) R = (mo%(4E) = "); 4(R) = E D 4(R);
+ creates a logical matri- base% on %i&isibility by E
+ canges only te non@%i&isible elements of 4 by multilying
tem by E en% + uses 4 as bot inut an% outut so e only nee% to mo%ify some elements of 4
Pr obl em di v v y( al t er nat i v es ol ut i on) s i ngl el i nes ol ut i on function S = %i&&y(SE) S(mo%(SE) = ") = S(mo%(SE) = ") D E; en% + same solution as abo&e but it reeats te mo%ulo comutation
Pr obl em squar e_wav e us i ngaf or l oop function s = suare2a&e(n) t = " I GDiJ1""" I GDi;
+ setu &ector accor%ing to te secs
s = >eros(1lengt(t));
+ initiali>e outut to "
for ii = 1I!I!Dn
+ run for Crst n o%% numbers (!E@1)
s = s . cos(iiDt@iJ!)Jii;
+ a%% te ne-t cosine term
en% en%
Pr obl em s quar e_wav e( al t er nat i v es ol ut i on) t r i c kycodewi t hnoex pl i c i tl oops function s = suare2a&e(n) t = " I GDiJ1""" I GDi; + setu &ector accor%ing to te secs i%- = (!D(1In)' @ 1);
+ maEe column &ector of Cst n o%% numbers (!E@1)
+ i%-Dt maEes a matri-; eac ro is (!E@1)Dt for a gi&en E + i%-Dones(si>e(t)) also maEes a matri-; eac element of ro E is Fust (!E@1) + sum %on te columns s = sum(sin(i%-Dt) J (i%-Dones(si>e(t)))1); en%
+ te secon% argument to sum is nee%e% in case n is 1 + remember tat sum(-) sums - along columns unless - is a ro &ectorN
Pr obl em my_pr i me us i ngaf or l oop function a = myrime(n) a = false; if n / 1
+ 1 is by %eCnition not rime
for ii = !Isrt(n)
+ see e-lanation belo
if mo%(nii)
return; en% en% a = true; en%
en% + - is rime if it is BOK %i&isible by all integers from ! to srt(-) + because factors a&e to come in airs @@ one bigger tan srt(-) an% + one smaller (or bot eual)
Pr obl em my _pr i me( al t er nat i v es ol ut i on) wi t hnoex pl i c i tl oops function rim = myrime() & = !Isrt(); & = &(rem(&) == ");
+ if is rime none of te remain%ers can be "
rim = lengt(&) AA ( = 1); + so if & as any elements is not rime en%
+ 1 is an%le% by te ( = 1) con%ition
So l u t i o nst oHo me wo r k7
Pr obl em i nt eger i z e t r adi t i onal s ol ut i onwi t has i ngl ei f el s ei f s t at ement function name = integeri>e(4) m- = ma-(4(I)); name = 'BOBT'; if m- e of largest element of 4 iclass = csum(ma-(4(I))/-).1*; en%
Pr obl em May 2015 function sub2Uay!"1H %ays = 0'Ku'; '8ri'; 'Vat'; 'Vun'; 'Uon'; 'Kue'; ':e%' ; for ii = 1I#1 m(ii)mont = 'Uay'; m(ii)%ate = ii; m(ii)%ay = %ays(rem(ii).1I); + .1 is nee%e% because " is an in&ali% in%een% en%
Pr obl em J une2015 t r adi t i onal s ol ut i onwi t haf or l oop function m = Wune!"1H %ays = 0 'Vun'; 'Uon'; 'Kue'; ':e%'; 'Ku'; '8ri'; 'Vat';
for ii = 1I#" mii1* = 'Wune'; mii!* = ii; mii#* = %ays(rem(ii).1I); en% en%
Pr obl em J une2015( al t er nat i v es ol ut i on) us i ngMATLABbui l t i nf unc t i onsi ns t ead function - = Wune!"1H + UaEe &ector of %ates for Wune !"1H t = %atetime(!"1H61I#"); + UaEe a cell array from te comonents of t - = cat(1mont(t'name')num!cell(%ay(t))%ay(t'sortname'))'; en%
Pr obl em c odei t t r adi t i onal s ol ut i on,l ook i ngatonec haratat i me function out = co%eit(in) for ii = 1Ilengt(in) if in(ii)
View more...
Comments