Homework Solutions - MATLAB

April 10, 2019 | Author: achueth | Category: Matrix (Mathematics), Algebra, Mathematical Objects, Physics & Mathematics, Mathematics
Share Embed Donate


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 = [001 00! 010 02!] " 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-n1: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  ## = #en_s.ures(nn)  ## = #en((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 = .udrnts(n)  = ones(n); 3 = [ 2" ; " 4"]; end 

Pr obl em checker boar d:

function  = chec$erord(n,#)   = ones(n,#); (1:2:n,2:2:#) = 0; (2:2:n,1:2:#) = 0; end 

Pr obl em r andomness :

function r = rndo#ness(li#it,n,#) r = fix(li#it " rnd(n,#))  1; end 

Pr obl em mt abl e:

function [t s] = #tle(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 : n1 : 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 < 191 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) ots = 'O''X''Y''Z''$'*; + ,reate cell array of otions i%- = 1 . sum(yr /= 01966191!"""!"1#); + ,alculate in%e- by comaring year to e%ge &alues gen = otsi%-*; 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 emty

c = @1 elseif ma-%im == 1 + oterise bot %im == 1 (since ma- == 1) @/ scalar c = "; elseif min%im == 1 + oterise if te 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%(%);

+ multilies te to %ims

y = @1 .(/=1) .(/1) .(min(%)/1) + eac a%%e% con%ition increases te anser by one en%

+ Bote tat te Crst to solutions are longer but easier to rea% an% un%erstan% tan tis one

Problem ol%er function a = ol%er(y1m1%1y!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(y1m1%1y!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 simly maEes a1 an% a! comarable

a = sign(a! @ a1);

+ sign() returns @1 " or 1 Fust at is nee%e%

en%

+ multilying by #66 or greater is nee%e% because of lea years

Problem mo&ies function can%o = mo&ies(r1min1%urmin1r!min!%urmin!) can%o = false; en%time = r1D6" . min1 . %urmin1;

+ con&ert times to minutes

starttime = r!D6" . min!; if en%time ing tat e %o not nee% to cecE eter te + buLer is emty or not since 0- buLer ill orE eiter ay + Ke tricEy art is o te lengt is an%le% :ile te buLer is + sorter tan !H buLer(1Ien%) is use% Once it reaces !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  = neigbor(&)  = 0; if min(si>e(&)) == 1

+ must be a &ector

for ii = 1Ilengt(&)@1

+ if lengt is less tan ! loo on't %o anyting

(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  = neigbor(&) if lengt(&) < ! ?? min(si>e(&)) = 1 + must be a &ector of at least to elements  = 0;   else  = abs(&(1Ien%@1)@&(!Ien%)); en%

+ taEe te %iLerence of to 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  = relace2me(&abc) if nargin < # b = ";

en% if nargin < G c = b; en%  = 0; for E = 1Ilengt(&); if &(E) == a

+ if a is foun%

 = 0bc; else

+ e insert b an% c at te en% of te current 

+ oterise

 = 0&(E); + e insert te 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  = relace2me(&abc) if nargin < # b = "; en% if nargin < G c = b; en%  = &;

+ maEe  te same as &

i = 1;

+ i is use% to in%e- into 

for &i = 1Ilengt(&) if &(&i) == a  = 0(1Ii@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 eiter 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

+ te column in%e- only starts at te current ro in%e-

s = s . 4(iiFF); 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(rrIen%));

+ sum a%%s u te elements rigt of te %iagonal

(inclusi&e) en%

+ in te 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(iiFF) / ii . FF

+ if te element is larger tan te sum of its in%e-es

foun% = 0foun%; ii FF; + a%% a ne ro to te outut matrien% en% en% en%

p r o bl e mo ne _p er _ n us i ngwhi l el oop function n = one2er2n(-) n = "; sum = "; ile sum < - AA n ero for o%% numbers tat is logical true

en% + note tat tis ill ut non@integers into o%%

Pr obl em di v v y function 4 = %i&&y (4E) R = (mo%(4E) = "); 4(R) = E D 4(R);

+ creates a logical matri- base% on %i&isibility by E

+ canges only te non@%i&isible elements of 4 by multilying

tem by E en% + uses 4 as bot inut an% outut 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(SE) S(mo%(SE) = ") = S(mo%(SE) = ") D E; en% + same solution as abo&e but it reeats te mo%ulo comutation

Pr obl em squar e_wav e us i ngaf or l oop function s = suare2a&e(n) t = " I GDiJ1""" I GDi;

+ setu &ector accor%ing to te secs

s = >eros(1lengt(t));

+ initiali>e outut to "

for ii = 1I!I!Dn

+ run for Crst n o%% numbers (!E@1)

s = s . cos(iiDt@iJ!)Jii;

+ a%% te 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 = suare2a&e(n) t = " I GDiJ1""" I GDi; + setu &ector accor%ing to te secs 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 %on te columns s = sum(sin(i%-Dt) J (i%-Dones(si>e(t)))1); en%

+ te secon% argument to sum is nee%e% in case n is 1 + remember tat sum(-) sums - along columns unless - is a ro &ectorN

Pr obl em my_pr i me us i ngaf or l oop function a = myrime(n) a = false; if n / 1

+ 1 is by %eCnition not rime

for ii = !Isrt(n)

+ see e-lanation belo

if mo%(nii)  

return; en% en% a = true; en%

en% + - is rime if it is BOK %i&isible by all integers from ! to srt(-) + because factors a&e to come in airs @@ one bigger tan srt(-) an% + one smaller (or bot eual)

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 = myrime() & = !Isrt(); & = &(rem(&) == ");

+ if  is rime none of te remain%ers can be "

rim = lengt(&) AA ( = 1); + so if & as any elements  is not rime en%

+ 1 is an%le% by te ( = 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 = csum(ma-(4(I))/-).1*; en%

Pr obl em May 2015 function sub2Uay!"1H %ays = 0'Ku'; '8ri'; 'Vat'; 'Vun'; 'Uon'; 'Kue'; ':e%' ; for ii = 1I#1 m(ii)mont = 'Uay'; m(ii)%ate = ii; m(ii)%ay = %ays(rem(ii).1I); + .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%'; 'Ku'; '8ri'; 'Vat';

for ii = 1I#" mii1* = 'Wune'; mii!* = ii; mii#* = %ays(rem(ii).1I); 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(!"1H61I#"); + UaEe a cell array from te comonents of t - = cat(1mont(t'name')num!cell(%ay(t))%ay(t'sortname'))'; 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

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF