Re: How to subtract two sets of data for a ListPlot
- To: mathgroup at smc.vnet.net
- Subject: [mg76749] Re: How to subtract two sets of data for a ListPlot
- From: Bill Rowe <readnewsciv at sbcglobal.net>
- Date: Sat, 26 May 2007 04:51:30 -0400 (EDT)
On 5/25/07 at 6:45 AM, dmilioto at comcast.com (chuck009) wrote:
>Hi all. Suppose I've calculated two sets of lists of the form for
>example:
>list1={{1,1},{2,2},{3,3}};
>list2={{1,1},{2,4},{3,9}};
>What's an efficient way to create a third list with the same
>x-values but with the y-values subtracted
>newlist={{1,0},{2,-2},{3,-6}};
>I realize I can manually subtract them in a For-loop, but I'm sure a
>one-line command using Map or some other construct can be used as
>well but can't figure it out.
Yes, you definitely want to avoid For loops. Here are two ways.
=46irst using pattern matching
In[3]:= Transpose[{list1, list2}] /. {{a_, b_}, {_, c_}} -> {a,
b - c}
Out[3]= {{1, 0}, {2, -2}, {3, -6}}
second using functional programming
In[4]:= MapThread[{First[#1], Last[#1 - #2]} & , {list1, list2}]
Out[4]= {{1, 0}, {2, -2}, {3, -6}}
And I am sure others will show you still more ways to do the
same thing
--
To reply via email subtract one hundred and four