You can use ReplacePart
data = RandomReal[{}, {2, 2}]
data = ReplacePart[data, {1, 2} -> -data[[1, 2]]]
You need to reassign the result back to data
You can make the above into a function if needed.
flip[data_, i_, j_] := ReplacePart[data, {i, j} -> -data[[i, j]]];
data = flip[data, 1, 2]
In your function, you also did something wrong. You can't assign to an input. So your statement table[[j, i]] = -table[[j, i]]
is writing to the input argument table
. If you really wanted to do as you show, then:
flip[table_, i_, j_] :=
Module[{myTable = table}, myTable[[i, j]] = -myTable[[i, j]];
myTable]
Now the local variable myTable
is used to modify, then it is returned back at the end. No need to use Return
since by default, the last expression is the result of the function. (do not use ;
at the end, else Null
is returned instead)
flip[table_,j_,i_]:=MapAt[(-1*#)&,table,{{j,i}}]
. However I thinkReplacePart
fits my situation better seeing as the "flip" only needs to be done at one location in the table rather than at many. Thanks @Nasser. $\endgroup$