In this notebook, we will learn how to compute the angle between two pictures modeled as matrices
(*Thiscodegeneratesatableofvalues,withtheouterloopiteratingoverthevariablekfrom1to10andtheinnerloopiteratingoverthevariablejfrom1to10.Foreachiterationoftheinnerloop,thevalueofk+jiscalculatedandaddedtothetable.TheMatrixFormfunctionisthenappliedtothetable,whichdisplaysthetableinamatrixformatwithrowsandcolumns.Thecodecreatesa10x10matrix,whereeachentryisthesumoftherowindexandthecolumnindex.*)Table[k+j,{k,1,10},{j,1,10}]//MatrixForm
Out[]//MatrixForm=
2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
(*Thiscodecreatesamatrix"M"withdimensions5x5,whereeachelementisthesumoftherowandcolumnindex.Forexample,theelementinthefirstrowandfirstcolumnwouldbe1+1=2.TheMatrixPlotfunctionisthenusedtodisplaythematrixvisually.*)M=Table[k+j,{k,1,5},{j,1,5}];MatrixPlot[M]
Out[]=
(*Thiscodecreatesamatrix"P"withdimensions5x5,whereeachelementisthesumofthesquareoftherowindexandtwicethecolumnindex.TheMatrixPlotfunctionisthenusedtodisplaythematrixvisually.Forexample,theelementinthefirstrowandfirstcolumnwouldbe1^2+21=1+2=3,theelementinthesecondrowandfirstcolumnwouldbe2^2+21=4+2=6.*)P=Table[k^2+2j,{k,1,5},{j,1,5}];MatrixPlot[P]
Out[]=
(*Thefirstlineofcode"v = Flatten[M]"takesthematrix"M"createdearlierandappliestheFlattenfunctiontoit,whichconvertsthematrixintoaone-dimensionallistofallitselements.Sotheresulting"v"isa1DlistofallelementsofthematrixMinrow-majororder.Thesecondlineofcode"w = Flatten[P]"doesthesamethingformatrix"P",resultingina1DlistofallelementsofthematrixPinrow-majororder.*)v=Flatten[M]w=Flatten[P]
Out[]=
{2,3,4,5,6,3,4,5,6,7,4,5,6,7,8,5,6,7,8,9,6,7,8,9,10}
Out[]=
{3,5,7,9,11,6,8,10,12,14,11,13,15,17,19,18,20,22,24,26,27,29,31,33,35}
(*Thecodeiscomputingtheanglebetweenthetwovectorsvandwinradians.Thedotproductoftwovectorsvandwisrepresentedbyv.w.Thedotproductoftwovectorsisequaltotheproductofthemagnitudeofthetwovectorsandthecosineoftheanglebetweenthem.Thecodethendividesthedotproductofvandwbytheproductofthesquarerootofthedotproductofvwithitselfandthesquarerootofthedotproductofwwithitselftogetthecosineoftheanglebetweenthetwovectors.ThisisthenpassedasanargumenttotheArcCosfunction,whichreturnstheangleinradians.Lastly,theNfunctionisusedtorepresentthenumberindecimalform.*)ArcCos[v.w/(Sqrt[v.v]Sqrt[w.w])]//N
Out[]=
0.255238