Message Boards Message Boards

1
|
7676 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 11 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 11 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
Posted 11 years ago
Yes! That was the problem! Thank you so much for your help, I really appreciate it!
POSTED BY: Amy Burnett
Posted 11 years ago
Ok. Here it is! It seems that the data is transformed but the graph plots the original data.
Attachments:
POSTED BY: Amy Burnett
Posted 11 years ago
Make up a very tiny little notebook containing half a dozen or a dozen pairs of your data assigned to a variable followed by your attempt at substititing. Start Mathematica fresh and evaluate just that once to see the In and Out from that.

Then either save that notebook and post it as an attachment OR select everything in the notebook and paste it all into a "code box" that you get by clicking on the <> near the right end of the second line.

That should show everyone your In and Out and exactly what it did and someone can get exactly your code back into their notebook, track down what is happening and show a tiny change to make it work for you and then they will use the same process to get the code back to you.
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 11 years ago
I have tried clearing x and y, and also tried using different variables, and nothing seems to work.  I'm pretty sure that my data, which is a list of pairs, is being transformed by these opperations, but when I plot the data that has been transformed, it does not plot transformed.  I am using ListPlot and I also tried Marco's method and it still does not transform the plot.
POSTED BY: Amy Burnett
 It seems to work fine.  Here is an example:
importedslit0data = Table[{Random[], Random[]}, {10}]


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

Does your importedslit0data have the form of a list of two element lists?
POSTED BY: David Reiss
Perhaps the poster had x and y previously defined?
i..e,,
Clear[x,y]
importedslit0data = Table[{Random[], Random[]}, {10}]
importedslit0data /. {x_, y_} -> {(x - 2.3585)*74.8, (y - 0.0191584)^2}

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

importedslit0data /. {x_, y_} :> {(x - 2.3585)*74.8, (y - 0.0191584)^2}
POSTED BY: W. Craig Carter
Yes, that also is a possiblity. So, suggestion to the poster, execute the following and see if your example then works:
Remove[x,y]
POSTED BY: David Reiss
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract