If I have like
{a,b}
and I want to get
{{a+1,b},{a-1,b},{a,b+1},{a,b-1}}
We don't care about the ordering of the list.Such as the {{a-1,b},{a+1,b},{a,b+1},{a,b-1}}
also is a valid list.
This is current method
MapAt[Reverse,
Transpose[{Distribute[Unevaluated@Plus[{1, -1}, {a, b}], List],
Riffle[{b, b}, {a, a}]}], {{2}, {4}}]
{{1+a,b},{a,1+b},{-1+a,b},{a,-1+b}}
Or this
Catenate@({Tuples[{Plus[#, {1, -1}], {#2}}],
Tuples[{{#}, Plus[#2, {1, -1}]}]} & @@ {a, b})
{{1+a,b},{-1+a,b},{a,1+b},{a,-1+b}}
Very ugly code.Are there more beautiful solution can do this?
lis = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; {a, b} + # & /@ lis
$\endgroup${a, b} + # & /@ Join[#, -#] &[IdentityMatrix[2]]
. ReplaceJoin[]
withRiffle[]
if desired. $\endgroup$Table[{a, b}, 4] + Join[#, -#] &[IdentityMatrix[2]]
$\endgroup$