I'm still a beginner with mathematica so maybe the solution is obvious, but for some reason my code that worked fine earlier doesn't work now. I haven't changed anything about it. I've restarted Mathematica and cleared all global variables to no avail. Here's what I'm working with:

flip[A_ , n_] :=
 Block[{lis1, lis2, S, size},
size = Dimensions[A];
lis1 = Most[ArrayRules[A]];
lis2 = RandomChoice[lis1, n] ;
lis2 = Map[ReplacePart[#, 2 -> -2] &, lis2[[1 ;; n]]];
S = SparseArray[lis2, {size[[1]], size[[1]]}, 0] + A ;];

It takes an adjacency matrix and flips a couple of entries. Or did anyway. Now nothing happens.

*One thing I still need to tweak is making sure I the entries are flipped symmetrically, i.e. if the (1,2) entry is picked by RandomChoice then I need to also add the (2,1) entry of lis1 so that S is a symmetric matrix. If someone could point me in the right direction I would appreciate it. (I think SymmetrizedArray might do it, but I'm not familiar enough with it).

EDIT: It all works now. I didn't change anything, and ran it today and it works fine. I really hope this is the last time I encounter these sort of Gremlins.

  • $\begingroup$ What do you mean "nothing happens"? You have defined a function. Are you calling it? $\endgroup$
    – bill s
    Commented Jan 28, 2016 at 4:31
  • $\begingroup$ Your function returns nothing. Remove the semicolon so that S = SparseArray[lis2, {size[[1]], size[[1]]}, 0] + A. $\endgroup$
    – dwa
    Commented Jan 28, 2016 at 8:05

2 Answers 2


Try this one.

flip[a_, n_Integer] := Module[{pos}
  , pos = Union[#, Reverse /@ #] &@RandomSample[SparseArray[a]["NonzeroPositions"], n]
  ; ReplacePart[a, Thread[pos -> -2 + Extract[a, pos]]]

It also makes the matrix symmetric.

flip[Array[b, {3, 3}], 2] // MatrixForm

enter image description here

You can put back RandomChoice in place of RandomSample if you want.


Simple syntactic errors. This works:

flip[a_List , n_] :=
 Block[{lis1, lis2, s, size},
size = Dimensions[a];
lis1 = Most[ArrayRules[a]];
lis2 = RandomChoice[lis1, n] ;
lis2 = Map[ReplacePart[#, 2 -> -2] &, lis2[[1 ;; n]]];
s = SparseArray[lis2, {size[[1]], size[[1]]}, 0] + a]

Don't use upper-case letters for variables or functions as they may conflict with Mathematica internal names.

Also, don't title your problem with "this doesn't work" types of uninformative titles. Use a title that will help someone find the solution later.

  • $\begingroup$ I have verbatim what you wrote and still nothing. I don't doubt that anything is wrong with it, but I have no idea what else the problem could be. $\endgroup$
    – TeeJay
    Commented Jan 28, 2016 at 4:12
  • $\begingroup$ @TeeJay. Copying and pasting DavidG.Stork's code directly and calling the function yields this. So I'm not sure what's going on with your Mathematica. (By the way, I was chastised recently for using Block instead of Module in this use case, so be aware that Block is dangerous.) $\endgroup$
    – march
    Commented Jan 28, 2016 at 4:52
  • $\begingroup$ @march I'm not sure either. I get no output, it just idles for a while. When it worked earlier it spit out a new matrix in no time. $\endgroup$
    – TeeJay
    Commented Jan 28, 2016 at 5:10
  • $\begingroup$ @TeeJay. And did you try it on a simple case like I posted (perhaps that exact same code)? Anyway, I don't know what could be the issue. It works for me. (By the way, are you aware the RandomChoice might choose the same element twice, and so sometimes less than n elements will be flipped?) $\endgroup$
    – march
    Commented Jan 28, 2016 at 5:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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