Convert decimal and negative values to integers
3 views (last 30 days)
Matt J on 4 Jul 2022
Something like this, perhaps?
a = 0:pi/100:2*pi;
y = sin(x);
Rik on 4 Jul 2022
Since all your values are between -1 and 1, you need a method to discretize your data to some arbitrary (but repeatable) indices. You have two general options:
- Have a linear lookup table. This will map all numbers lineraly to the closest integer.
- Have a non-linear lookup table. This allows you to decide what data ranges should have more precision.
Below I will implement the simplest version of option 1 I can think of. If you want to increase performance, interp1 may be required.
discretize_values([-1 0 1],3)
Now we can apply this to your curve. What I show here is how you can set the precision to influence how jagged your graph will look. Set a higher number for a smoother curve. The mapping back to non-integers should of course be removed if you are planning to use this for indexing.
y2=2*((y2-1)/(precision-1))-1; % map back to non-integers
% map (-1,1) to 1:precision
if any(in>1) || any(in<-1)
error('data outside expected range')