5
$\begingroup$

I have this list of x, y-pairs l={{x1, y1}, {x2, y2}, ...}. Now I need to flip the sign of the y values only before plotting. What is the easiest way to do this modification?

$\endgroup$
8
  • 1
    $\begingroup$ {1, -1} # & /@ l or Transpose@{#[[1]], -#[[2]]} &@Transpose[l] $\endgroup$
    – BlacKow
    Commented May 13, 2016 at 15:39
  • $\begingroup$ Welcome! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – user9660
    Commented May 13, 2016 at 15:50
  • 1
    $\begingroup$ Or even faster Transpose[{1, -1} # &@Transpose[l]] $\endgroup$
    – BlacKow
    Commented May 13, 2016 at 15:50
  • 1
    $\begingroup$ Or MapAt[-# &, l, {All, 2}]. Or {#1, -#2} & @@@ l. $\endgroup$
    – march
    Commented May 13, 2016 at 15:51
  • $\begingroup$ This is almost certainly a duplicate. I will see if I can find it. $\endgroup$
    – march
    Commented May 13, 2016 at 15:55

7 Answers 7

11
$\begingroup$

Dot works well here.

pts = {{x1, y1}, {x2, y2}, {x3, y3}, {x4, y4}};

pts.{{1, 0}, {0, -1}}
{{x1, -y1}, {x2, -y2}, {x3, -y3}, {x4, -y4}}

In version 10.1.0 under Windows x64 this is twice as fast as BlacKow's fastest method:

l = RandomReal[{0, 1}, {1000000, 2}];

Transpose[{1, -1} Transpose[l]]   // RepeatedTiming // First

l.{{1, 0}, {0, -1}}               // RepeatedTiming // First
0.0133

0.0072
$\endgroup$
5
  • $\begingroup$ I was so sure that you can't do this, that it will cause some dimensions mismatch, so I haven't even tried this... I'll add it to my benchmark later. Thanks! $\endgroup$
    – BlacKow
    Commented May 14, 2016 at 3:12
  • 1
    $\begingroup$ I like this. Nice to see you using dot products! :) $\endgroup$ Commented May 14, 2016 at 5:13
  • $\begingroup$ @J.M. There is a pretty good chance you taught me this but my memory is too poor to remember. $\endgroup$
    – Mr.Wizard
    Commented May 14, 2016 at 6:29
  • $\begingroup$ @J.M. Do you feel that this is a duplicate of (38138)? I do, but not strongly, and I did not wish to act alone. $\endgroup$
    – Mr.Wizard
    Commented May 15, 2016 at 3:38
  • $\begingroup$ Somehow I recall another thread with the exact same problem of reflecting, but the link escapes me at the moment. BTW, I'm sure you know this, but for general scaling and reflection, one could use DiagonalMatrix[], or even ScalingTransform[]. $\endgroup$ Commented May 15, 2016 at 4:10
12
$\begingroup$

Little benchmark to compare suggested methods:

l = RandomReal[{0, 1}, {1000000, 2}];
b1[l_List] := {1, -1} # & /@ l;
b2[l_List] := Transpose@{#[[1]], -#[[2]]} &@Transpose[l];
b3[l_List] := Transpose[{1, -1} Transpose[l]];
m1[l_List] := MapAt[-# &, l, {All, 2}];
m2[l_List] := {#1, -#2} & @@@ l;
ch1[l_List] := l /. {a_, b_} :> {a, -b};
g1[l_List] := l ConstantArray[{1, -1}, Length@l];
w1[l_List] := l.{{1, 0}, {0, -1}};
fb1[l_List] := Thread[{l[[;; , 1]], -l[[;; , 2]]}];

#2 -> #1 & @@@ SortBy[#, #[[1]] &] &@({#1, #2} & @@@Transpose@{First@AbsoluteTiming[#[l]] & /@ #, #}&@{b1, b2, b3,m1, m2, ch1, g1,w1,fb1})

{w1 -> 0.007054, b3 -> 0.016369, b2 -> 0.020753, b1 -> 0.155566, fb1 -> 0.25016, g1 -> 0.323931, m2 -> 1.05527, m1 -> 1.13127, ch1 -> 1.32451}

$\endgroup$
10
  • $\begingroup$ b3 is the last thing I would have used without much thinking, neat to see how much faster it is though. $\endgroup$
    – N.J.Evans
    Commented May 13, 2016 at 16:28
  • $\begingroup$ @FrankBreitling Usually you create a new list in Mathematica and not modify existing one. Your method l=Thread[{l[[1]], -l[[2]]}] doesn't yield desired result. $\endgroup$
    – BlacKow
    Commented May 13, 2016 at 17:02
  • $\begingroup$ you could write b3 as Transpose[{1, -1} Transpose@l] (+1) $\endgroup$
    – kglr
    Commented May 13, 2016 at 17:59
  • $\begingroup$ @kglr Indeed... not sure why I came up with that one lol.. Fixed $\endgroup$
    – BlacKow
    Commented May 13, 2016 at 18:01
  • 1
    $\begingroup$ @FrankBreitling I updated my answer with your method. $\endgroup$
    – BlacKow
    Commented May 16, 2016 at 22:21
4
$\begingroup$
list = {{x1, y1}, {x2, y2}, {x3, y3}, {x4, y4}};

Using Query

Query[All, {2 -> Minus}] @ list

{{x1, -y1}, {x2, -y2}, {x3, -y3}, {x4, -y4}}

Using MapAt

MapAt[Minus, {All, 2}] @ list

{{x1, -y1}, {x2, -y2}, {x3, -y3}, {x4, -y4}}

$\endgroup$
3
$\begingroup$
list = {{x1, y1}, {x2, y2}, {x3, y3}, {x4, y4}};

Using Threaded (new in 13.1)

list * Threaded[{1, -1}]

{{x1, -y1}, {x2, -y2}, {x3, -y3}, {x4, -y4}}

$\endgroup$
3
$\begingroup$

Using Cases and PauliMatrix[3]:

pts = {{x1, y1}, {x2, y2}, {x3, y3}, {x4, y4}};

Cases[pts, v_ :> v . PauliMatrix[3]]

(*{{x1, -y1}, {x2, -y2}, {x3, -y3}, {x4, -y4}}*)
$\endgroup$
3
$\begingroup$

I am glad to see that people are using Threaded

I am surprised nobody mentioned the use of Inner

Grabbing the list from @eldo

list = {{x1, y1}, {x2, y2}, {x3, y3}, {x4, y4}};

This

Inner[Times, list, {1, -1}, List]

returns

{{x1, -y1}, {x2, -y2}, {x3, -y3}, {x4, -y4}}

We can, also, use Splice

Cases[list, x_ :> {First@x, Splice@Rest@-x}]

{{x1, -y1}, {x2, -y2}, {x3, -y3}, {x4, -y4}}

And ApplyTo

list = {{x1, y1}, {x2, y2}, {x3, y3}, {x4, y4}};
list //= ReplaceAt[x_ :> -x, {All, 2}];
list

giving

{{x1, -y1}, {x2, -y2}, {x3, -y3}, {x4, -y4}}

$\endgroup$
2
$\begingroup$
list = {{x1, y1}, {x2, y2}, {x3, y3}, {x4, y4}};

SubsetMap[-# &, list, {All, 2}]
SequenceCases[list, {{a_, b_}} :> {a, -b}]
list . ReflectionMatrix[{0, 1}]

Result:

{{x1, -y1}, {x2, -y2}, {x3, -y3}, {x4, -y4}}

$\endgroup$

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.