Group Abstract Group Abstract

Message Boards Message Boards

How to use iteration functions?

Posted 9 years ago

Hello,

I am still learning how to use mathematica correctly and in my latest projects I encountered an issue regarding the repetition of an action over and over again and how to compute such function. For example, in this project I have a code separated in two pieces. First, the initialization:

x = 2;
n = x^2;                                                              


meme = Table[
   Table[RandomInteger[{1, 7}], {j, 1, 5}], {i, 1, 
    n}];                      
MatrixForm[meme]

which just set the parameters and create a matrix of social attributes.

Then, in second part, we create a grid matrix with each vertex facing a random adjacent vertex, and when two vertices face each other they exchange their social attributes.

G = GridGraph[{x, x}];
M0 = Normal[AdjacencyMatrix[G]];
P = Table[Flatten[Position[M0[[i]], 1]], {i, 1, n}];                
M = ConstantArray[
   0, {n, n}];                                                        \

For[i = 1, i <= n, i++, M[[i, RandomChoice[P[[i]]]]] = 1];             
AdjacencyGraph[M, 
 VertexCoordinates -> 
  If[EvenQ[x] === True, 
   Flatten[Table[{i, -j}, {j, -(x - 1), x - 1, 2}, {i, -(x - 1), 
      x - 1, 2}], 1], 
   Flatten[Table[{i, -j}, {j, -(x - 1)/2, (x - 1)/2}, {i, -(x - 1)/
       2, (x - 1)/2}], 1]], DirectedEdges -> True, 
 VertexLabels -> "Name"]    
MatrixForm[M];
MA = Flatten[
   Table[If[M[[i, j]] === 1, 
     If[M[[i, j]] === M[[j, i]], List[{i, j}], 
      Unevaluated[Sequence[]]], Unevaluated[Sequence[]]], {i, 1, 
     n}, {j, 1, n}]]; 
Con = If[MA === {}, 0, DeleteDuplicates[MA]];            
L1 = Flatten[
   Table[Take[Con, {2*i - 1}], {i, 1, Length[Con]/2}]];          
L2 = Flatten[
   Table[Take[Con, {2*i}], {i, 1, Length[Con]/2}]];                
Table[{L1[[i]], L2[[i]]}, {i, 1, Length[Con]/2}]
inter = Table[
  If[Flatten[
     Position[
      Abs[meme[[L1[[i]]]] - meme[[L2[[i]]]]], _?Positive]] === {}, 0, 
   RandomChoice[
    Flatten[Position[
      Abs[meme[[L1[[i]]]] - meme[[L2[[i]]]]], _?Positive]]]], {i, 1, 
   Length[Con]/2}]     
For[i = 1, i <= Length[Con]/2, i++, If[inter[[i]] === 0, meme,
   meme[[L1[[i]], inter[[i]]]] = 
    meme[[L2[[i]], inter[[i]]]] = 
     RandomInteger[{Min[meme[[L1[[i]], inter[[i]]]], 
        meme[[L2[[i]], inter[[i]]]]], 
       Max[meme[[L1[[i]], inter[[i]]]], 
        meme[[L2[[i]], inter[[i]]]]]}]]];
MatrixForm[meme]

The code, even though it might not be in its simplest form, is working correctly and with the absence of bugs. However, in order to run several steps of the process, I have to, once I ran the initialization, run the second part manually over and over again which is not practical.

I tried to insert a "For" function depending on a parameter k that I would later "Manipulate" to make the whole thing more interactive but I couldn't find anything inside the second block that could depend on this parameter k.

I have been stuck with this issue for a moment now, and it has been blocking some of my other projects as well, therefore I am seeking help here on this forum if any of you could help me mastering all the functionalities of this software.

Thank you for your help.

POSTED BY: Max Mayca
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard