Group Abstract Group Abstract

Message Boards Message Boards

1
|
8.7K Views
|
9 Replies
|
9 Total Likes
View groups...
Share
Share this post:

How to subtract a number from all the y values in a data set

Posted 12 years ago
Right now I am using this:

importedslit0data /. {x_,     y_} -> {(x - 2.3585)*74.8, (y - 0.0191584)^2};

and I am trying to subtract 2.3585 from x, then multiply by 74.8 and I am trying to subtract 0.0191584 from y and square.
The subtracting does not seem to be working.
Any suggestions?
POSTED BY: Amy Burnett
9 Replies
Posted 12 years ago
AHA! Now the actual problem is clear.
importedslit0data /. {x_, y_} -> {(x - 2.3585)*74.8, (y - 0.0246623)^2}
does, as you see, transform the data.

BUT that does not then store the result back into importedslit0data, it just transforms it and displays the result and nothing more.
What you have is sort of like you say z=2 and then you say z^2 and Mathematica prints 4 and then you say
Plot z and mathematica plots 2, because squaring didn't also mean to store the new 4 back into z.

So try this.
importedslit0data = importedslit0data /. {x_, y_} -> {(x - 2.3585)*74.8, (y - 0.0246623)^2}

ListPlot[importedslit0data]
That tansforms it, stores the result back into the original variable, losing the original numbers now, and then should plot the new numbers.

So much easier and quicker to get to a solution when we can actually see "all" the code.
Thank you for that

In your defense, this is a relatively common trap for new users to fall into. Again and again I have seen others read the documentation and seem to come to exactly the same conclusion that I think you did, that when the documentation says "it replaces what matches the pattern" that it is assumed it assigns the new value to the existing variable.
POSTED BY: Bill Simpson
Might also be done with pure functions:
{(#[[1]] - 2.3585)*74.8, (#[[2]] - 0.0191584)^2} & /@ importedslit0data

This tends to be much faster. See for example:
 (*Generate Data*)
 In[13]:= importedslit0data = Table[{Random[], Random[]}, {10^7}];
 
 (*Pure function*)
 In[15]:= ({(#[[1]] - 2.3585)*74.8, (#[[2]] - 0.0191584)^2} & /@
     importedslit0data); // AbsoluteTiming
 
 Out[15]= {2.005758, Null}
 
(*Rule based*)
In[14]:= (importedslit0data /. {x_,
      y_} -> {(x - 2.3585)*74.8, (y - 0.0191584)^2}); // AbsoluteTiming

Out[14]= {107.027852, Null}


M.
POSTED BY: Marco Thiel
Posted 12 years ago
Ok. Here it is! It seems that the data is transformed but the graph plots the original data.
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard
Be respectful. Review our Community Guidelines to understand your role and responsibilities. Community Terms of Use