ArrayPlotDoubleLoopTripleLoop.nb

.pdf
School
Merced College**We aren't endorsed by this school
Course
MICROECONO 101
Subject
Computer Science
Date
Dec 16, 2024
Pages
11
Uploaded by AdmiralInternetHummingbird55
Double For-Loop: visual representa-tionFor[i=1, i<=IMax, i++,For[j=1, j<=JMax, j++,Task[i,j];];];A) Make ArrayPlot for single i,j valueIn[1]:=NX=10;(*Size of Table/2x2Array/Matrix*)i=1;(*row location*)j=2;(*column location*)V1=Array[1 &,(i-1) *NX+j] (*Vector of 1s up to current i,j location*)V2=Array[0 &, NX^2- ((i-1) *NX+j)](*Vector of 0s aftercurrent i,j location*)V12=Join[V1, V2]Length[V12] /NX^2Matrix12=Partition[V12, NX]Matrix12i〛〚j〛 =2;(*Set current location i,j to 2*)ArrayPlot[Matrix12, MeshTrue,ColorRules→ {1Pink, 0Yellow, 2Gray}, ImageSize200]Out[4]={1, 1}
Background image
Out[5]={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}Out[6]={1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}Out[7]=1Out[8]={{1, 1, 0, 0, 0, 0, 0, 0, 0, 0},{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}Out[10]=B) Make ArrayPlot dynamic across all ij valuesMethod A: Make a list of NX^2 plots2ArrayPlot_DoubleLoop_TripleLoop.nb
Background image
In[11]:=(*Make list of all images and store in variable called ArrayPlotij*)ArrayPlotij=Reap[For[i=1, iNX, i++,For[j=1, jNX, j++,V1=Array[1 &,(i-1) *NX+j];(*Vector of 1s up to current i,j location*)V2=Array[0 &, NX^2- ((i-1) *NX+j)];(*Vector of 0s aftercurrent i,j location*)V12=Join[V1, V2];Matrix12=Partition[V12, NX];Matrix12i〛〚j〛 =2;(*Set current location i,j to 2*)Sow[ArrayPlot[Matrix12, MeshTrue, ColorRules→ {1Pink, 0Yellow,2Gray}, ImageSize200, PlotLabelStyle[StringJoin["{i,j} = {", ToString[i], ",", ToString[j], "}"], Black, 20]]];];];]〚2〛〚1;Length[ArrayPlotij]ArrayPlotij10Out[12]=100Out[13]={i,j} = {1,10}Now inspect Animate[ ] specifications:https://reference.wolfram.com/language/ref/Animate.htmlArrayPlot_DoubleLoop_TripleLoop.nb 3
Background image
In[14]:=Animate[Plot[BesselJ[n, x],{x, 0, 10}, PlotRange0.7],{n, 1, 5, 1}, AnimationRunningFalse]Out[14]=n246810-0.6-0.4-0.20.20.40.6In[24]:=Locij=30ArrayPlotijLocijOut[24]=30Out[25]={i,j} = {3,10}4ArrayPlot_DoubleLoop_TripleLoop.nb
Background image
In[23]:=Animate[ArrayPlotijLocij,{Locij, 1, NX^2, 1}, AnimationRunningFalse]Out[23]=Locij{i,j,k} = {6,1,1}Method B: parameterize in terms of both i and j location - make the Array-Plot dynamically based upon a specific {i,j}ArrayPlot_DoubleLoop_TripleLoop.nb 5
Background image
In[20]:=i=10j=2;ArrayPlot[Partition[Join[Array[1 &,(i-1) *NX+j-1],{2}, Array[0 &, NX^2- ((i-1) *NX+j)]], NX],MeshTrue, ColorRules→ {1Pink, 0Yellow, 2Gray},ImageSize200, PlotLabelStyle[StringJoin["{i,j} = {", ToString[i], ",", ToString[j], "}"], Black, 20]]""Out[20]=10Out[22]={i,j} = {10,2}6ArrayPlot_DoubleLoop_TripleLoop.nb
Background image
In[19]:=Animate[ArrayPlot[Partition[Join[Array[1 &,(i-1) *NX+j-1],{2}, Array[0 &, NX^2- ((i-1) *NX+j)]], NX], MeshTrue,ColorRules→ {1Pink, 0Yellow, 2Gray}, ImageSize200, PlotLabelStyle[StringJoin["{i,j} = {", ToString[i], ",", ToString[j], "}"], Black, 20]],{i, 1, NX, 1},{j, 1, NX, 1}, AnimationRunningFalse]Out[19]=ij{i,j} = {6,4}Method C: A mixture of A & B, in which we parameterize the plot in terms of a net Locij variable - then deduce {i,j} from LocijIn[16]:=(*Deduce{i,j}from Locij*)Locij=10;i=1+IntegerPart[(Locij-1) /NX]j=Mod[Locij-1, NX] +1Out[17]=1Out[18]=10ArrayPlot_DoubleLoop_TripleLoop.nb 7
Background image
In[15]:=(*Copy Method B implmentation from above-replace each i and j with its Locij parameterization*)Animate[ArrayPlot[Partition[Join[Array[1 &,(1+IntegerPart[(Locij-1) /NX] -1) *NX+(Mod[Locij-1, NX] +1) -1],{2}, Array[0 &, NX^2-((1+IntegerPart[(Locij-1) /NX] -1) *NX+ (Mod[Locij-1, NX] +1))]], NX],MeshTrue, ColorRules→ {1Pink, 0Yellow, 2Gray}, ImageSize200,PlotLabelStyle[StringJoin["{i,j} = {", ToString[1+IntegerPart[(Locij-1) /NX]],",", ToString[Mod[Locij-1, NX] +1], "}"], Black, 20]],{Locij, 1, NX^2, 1}, AnimationRunningFalse]Out[15]=Locij{i,j} = {1,1}C) Triple-loop extension: Make ArrayPlot dynamic across all ijk valuesMethod A: Make a list of NX^3 plots - indicate k-value using variable border color8ArrayPlot_DoubleLoop_TripleLoop.nb
Background image
In[35]:=(*Make list of all images and store in variable called ArrayPlotij*)ArrayPlotij=ReapFork=1, kNX, k++,Fori=1, iNX, i++,Forj=1, jNX, j++,V1=Array[1 &,(i-1) *NX+j];(*Vector of 1s up to current i,j location*)V2=Array[0 &, NX^2- ((i-1) *NX+j)];(*Vector of 0s aftercurrent i,j location*)V12=Join[V1, V2];Matrix12=Partition[V12, NX];Matrix12i〛〚j〛 =2;(*Set current location i,j to 2*)(*From demo below-How to join two Style[]d strings*)PlotLabelijk=Apply[StringJoin, ToString[#, StandardForm]&/@ {"{i,j,k} = {",ToString[i], ",", ToString[j], ",", Style[ToString[k],Blend[{GrayLevel[0.9*k/NX], Green}, .3], Bold, Large], "}"}];Sow[ArrayPlot[Matrix12, MeshTrue,MeshStyleDirective[Blend[{GrayLevel[0.9*k/NX], Green}, .2], AbsoluteThickness[8]],ColorRules→ {1Pink, 0Yellow, 2Gray},ImageSize300, PlotLabelPlotLabelijk]];;;;〚2〛〚1;Length[ArrayPlotij]ArrayPlotij1Out[36]=1000ArrayPlot_DoubleLoop_TripleLoop.nb 9
Background image
Out[37]={i,j,k} = {1,1,1}In[26]:=Animate[ArrayPlotijLocij,{Locij, 1, NX^3, 1}, AnimationRunningFalse]Out[26]=Locij{i,j,k} = {1,1,1}How to join two Style[]d stringshttps://mathematica.stackexchange.com/questions/10990/how-to-join-two-styled-strings10ArrayPlot_DoubleLoop_TripleLoop.nb
Background image
In[27]:=(*Doesn't work*)i=1;j=2;k=2;StringJoin["{i,j,k} = {", ToString[i], ",", ToString[j],",", Style[ToString[k], GrayLevel[0.8*k/NX], Bold], "}"]StringJoin:String expected at position 2 in{i,j,k} = {1,2,<>2<> }.Out[30]={i,j,k} = {1,2,<>2<> }In[31]:=(*Following example from above-use: Apply[StringJoin,ToString[#,StandardForm]&/@items]*)i=1;j=2;k=1;PlotLabelijk=Apply[StringJoin, ToString[#, StandardForm]&/@ {"{i,j,k} = {", ToString[i], ",",ToString[j], ",", Style[ToString[k], GrayLevel[0.8*k/NX], Bold], "}"}]Out[34]={i,j,k} = {1,2,1}ArrayPlot_DoubleLoop_TripleLoop.nb 11
Background image