Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm trying to write a code to "random" move in a matrix but it can't go to a certain position if it has a certain value (in this case I'm using the number 2). How do I make it to skip all other If conditions if one of them is found True? Another thing, is it possible to store the RandomChoise in a variable to be used later? Thank you

If[And[matrix[[i+1,j]]!=2,matrix[[i-1,j]]!=2,matrix[[i,j+1]]!=2,matrix[[i,j-1]]!=2], RandomChoice[{up,down,right,left}]]

If[And[matrix[[i+1,j]]!=2,matrix[[i-1,j]]!=2,matrix[[i,j+1]]=2,matrix[[i,j-1]]!=2], RandomChoice[{up,down,left}]]

If[And[matrix[[i+1,j]]!=2,matrix[[i-1,j]]!=2,matrix[[i,j+1]]!=2,matrix[[i,j-1]]=2], RandomChoice[{up,down,right}]]

If[And[matrix[[i+1,j]]!=2,matrix[[i-1,j]]=2,matrix[[i,j+1]]!=2,matrix[[i,j-1]]!=2], RandomChoice[{down,right,left}]]

If[And[matrix[[i+1,j]]=2,matrix[[i-1,j]]!=2,matrix[[i,j+1]]!=2,matrix[[i,j-1]]!=2], RandomChoice[{up,right,left}]]

If[And[matrix[[i+1,j]]=2,matrix[[i-1,j]]=2,matrix[[i,j+1]]!=2,matrix[[i,j-1]]!=2], RandomChoice[{right,left}]]

If[And[matrix[[i+1,j]]!=2,matrix[[i-1,j]]=2,matrix[[i,j+1]]!=2,matrix[[i,j-1]]=2], RandomChoice[{down,right}]]

If[And[matrix[[i+1,j]]!=2,matrix[[i-1,j]]=2,matrix[[i,j+1]]=2,matrix[[i,j-1]]!=2], RandomChoice[{down,left}]]

If[And[matrix[[i+1,j]]=2,matrix[[i-1,j]]!=2,matrix[[i,j+1]]!=2,matrix[[i,j-1]]=2], RandomChoice[{up,right}]]

If[And[matrix[[i+1,j]]=2,matrix[[i-1,j]]!=2,matrix[[i,j+1]]=2,matrix[[i,j-1]]!=2], RandomChoice[{up,left}]]

If[And[matrix[[i+1,j]]!=2,matrix[[i-1,j]]!=2,matrix[[i,j+1]]=2,matrix[[i,j-1]]=2], RandomChoice[{up,down}]]

If[And[matrix[[i+1,j]]=2,matrix[[i-1,j]]=2,matrix[[i,j+1]]!=2,matrix[[i,j-1]]=2], RandomChoice[{right}]]

If[And[matrix[[i+1,j]]=2,matrix[[i-1,j]]=2,matrix[[i,j+1]]=2,matrix[[i,j-1]]!=2], RandomChoice[{left}]]

If[And[matrix[[i+1,j]]!=2,matrix[[i-1,j]]=2,matrix[[i,j+1]]=2,matrix[[i,j-1]]=2], RandomChoice[{down}]]

If[And[matrix[[i+1,j]]=2,matrix[[i-1,j]]!=2,matrix[[i,j+1]]=2,matrix[[i,j-1]]=2], RandomChoice[{up}]]
share|improve this question
    
Check out Which... –  kale Jun 4 at 18:33
    
And also remember that = is Set and == is Equal used for comparison purposes. –  kale Jun 4 at 18:36

4 Answers 4

This is more than you've asked but tell me if this works for you. I've added artificial border with 2s around the world :)

init[] := (dim = {10, 10};
  world = ArrayPad[RandomInteger[{1, 10}, dim], {1, 1}, 2];
  known = ArrayPad[ConstantArray[0, dim], {1, 1}, 2];
  p = {2, 2};
  (known[[##]] = world[[##]]) & @@ p;)

init[]
MatrixForm /@ {world, known}

enter image description here

    While[
     True,     
     Composition[
      p = #[[1]]; (known[[##]] = world[[##]]) & @@ p; 
      If[#[[2]] == 10, Break[]]; &,
      If[# === {}, Break[], RandomChoice[#]] &,
      DeleteCases[#, {_, 2}] &,
      Thread[{#, Extract[world, #]}] &
     ][
       # + p & /@ {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}
      ]
     ]

enter image description here


Dynamic fun:

init[];
Dynamic@MatrixForm@known

While[
 True,     
 Composition[
   p = #[[1]]; (known[[##]] = Style[world[[##]], Bold, 15, Red]) & @@ p; 
     Pause@.1;  If[#[[2]] == 10, Break[]]; &,
   If[# === {}, Break[], RandomChoice[#]] &,
   DeleteCases[#, {_, 2}] &,
   (known[[##]] = Style[world[[##]], Bold, 15]) & @@ p; 
   Thread[{#, Extract[world, #]}] &
   ][
  # + p & /@ {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}
  ]]

enter image description here

share|improve this answer
    
Kuba's answer very much reminded me of Christian Jacob's works about Evolutionary Algorithms. Some fascinating Mathematica examples examples can be downloaded here: pages.cpsc.ucalgary.ca/~jacob/Evolvica/… –  eldo Jun 4 at 20:27
    
@AndréF. The DeleteCases part is responsible for dropping walls. The direction is chosen from the remaining parts. You must skip this part and include your code then. –  Kuba Jun 9 at 17:30
1  
@AndréF. Try this Composition[ With[{val = #[[2]], pos = #[[1]]}, (known[[##]] = world[[##]]) & @@ pos; Switch[val, 10, p = pos; Break[], 2, {}, _, p = pos]] &, RandomChoice, Thread[{#, Extract[world, #]}] & ]. Materials about what? :) –  Kuba Jun 9 at 18:29
    
@AndréF. I find those tutorials helpful. Choose one you want to focus at the moment and parse it. –  Kuba Jun 9 at 18:36
1  
@AndréF. Switch those two compositions ;) Please delete old not relevant comments. –  Kuba Jun 9 at 18:44

I like Pick for this:

Pick[
  {up, down, left, right},
  Extract[matrix, {{i + 1, j}, {i - 1, j}, {i, j - 1}, {i, j + 1}}],
  x_ /; x != 2
 ] // RandomChoice
share|improve this answer
a = matrix[[i + 1, j]];
b = matrix[[i - 1, j]];
c = matrix[[i, j + 1]];
d = matrix[[i, j - 1]];

Which[
 And[a != 2, b != 2, c != 2, d != 2], RandomChoice[{up, down, right, left}],
 And[a != 2, b != 2, c == 2, d != 2], RandomChoice[{up, down, left}],
 (* ... *)
 And[a == 2, b != 2, c == 2, d == 2], up
 ]

If you want to store the choice in a variable just precede the above with

variable = Which[ ... ]

Last but not least:

 RandomChoice[{up}]]

is redundant.

In case you want to implement a kind of random walk with this, have a look here: http://demonstrations.wolfram.com/TurtleGraphics/

share|improve this answer
    
My objective is no random walk until it find a certain value. There will be two different tables, one representing what is known by the subject and another representing the world. When the subject walk it has to update the table containing what is known by him and in case it find a wall (the number two) it should update and return to previous position. –  André F. Jun 4 at 19:15

how about something like this:

  choices=Flatten@{If[ matrix[[i+1,j]]!=2 , up ,{}],
           If[ matrix[[i-1,j]]!=2 , down ,{}],
           If[ matrix[[i,j-1]]!=2 , left ,{}],
           If[ matrix[[i,j+1]]!=2 , right ,{}]}
  If[choices!={},RandomChoice[choices]]

or even

  choices=Flatten@MapThread[If[ #1!=2,#2,{} ]&,
      {{matrix[[i+1,j]],matrix[[i-1,j]],matrix[[i,j-1]],matrix[[i,j+1]]},
        {up,             down,            left,           right}}]
  If[choices!={},RandomChoice[choices]]

note in this case the first approach is probably better since your next problem will be throwing errors when you hit the edge so you should do:

   If[ i>1 && matrix[[i-1,j]]!=2 , down ,{}]

etc.

share|improve this answer
    
pure intuition tells me that your approach would give another result than the definition of the OP. You should, therefore, run it over a short example matrix and compare. –  eldo Jun 4 at 19:57
    
the only issue is I failed to check the case of no choices, will fix. –  george2079 Jun 4 at 20:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.