# Flipping random elements in a matrix

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.

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

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


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.

• 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. – TeeJay Jan 28 '16 at 4:12
• @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.) – march Jan 28 '16 at 4:52
• @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. – TeeJay Jan 28 '16 at 5:10
• @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?) – march Jan 28 '16 at 5:27