Convert decimal and negative values ​​to integers

There are x and y coordinates that I need to convert to integer values ​​while preserving the meaning of the graph
a = 0:pi/100:2*pi;
x=cos(a);
y = sin(x);
plot(x,y) % first graph
% plot(a,y) % second graph
Thanks in advance

12 comentarios

Dyuman Joshi
Dyuman Joshi el 4 de Jul. de 2022
By preserve I assume the plot must remain same but the values must be converted to integers? And if so, how do you want to convert values to integers?
Lev Mihailov
Lev Mihailov el 4 de Jul. de 2022
@Dyuman Joshi yes, the values ​​must be integers. By conversion, I thought about sorting values, replace fractional values ​​with integers
The only non-negative integers you will get out of sin and cos are 0 and 1.
Do you mean you want to preserve the shape of the graph?
Why do you want to only leave positive integers? Are you planning to use these values as indices?
a=linspace(0,2*pi,200);
x=cos(a);
y=sin(a);
plot(x,y)
hold on
plot(a,y)
hold off
daspect([1 1 1])
Lev Mihailov
Lev Mihailov el 4 de Jul. de 2022
@Rik Yes, I only want to keep the graph shape. Yes this data will be used as matrix indexes
Dyuman Joshi
Dyuman Joshi el 4 de Jul. de 2022
"replace fractional values ​​with integers"
What do you mean by this?
Lev Mihailov
Lev Mihailov el 4 de Jul. de 2022
@Dyuman Joshi I mean, 0.3 0.5 0.7 is replaced by 1 2 3, and -5.4 1.2 4.5 is replaced by 1 3 4
Dyuman Joshi
Dyuman Joshi el 4 de Jul. de 2022
Only these specific values?
Lev Mihailov
Lev Mihailov el 4 de Jul. de 2022
@Dyuman Joshi well i would like a generic method
Dyuman Joshi
Dyuman Joshi el 4 de Jul. de 2022
Don't you think you should think about it? Especially when it look like a random method.
Lev Mihailov
Lev Mihailov el 4 de Jul. de 2022
@Dyuman Joshi I thought for a week and there was no good thought
Dyuman Joshi
Dyuman Joshi el 4 de Jul. de 2022
Ok. So, What is it that you are trying to do in the first place? How did you come up with the conversions you mentioned above?
Lev Mihailov
Lev Mihailov el 4 de Jul. de 2022
@Dyuman Joshi take min and max values, create an array, from -5.4 ; 1.2 to 4.5, fill array from 1 to n(up to 4), where 1 is the smallest value and 4 is the maximum

Iniciar sesión para comentar.

Respuestas (2)

Something like this, perhaps?
a = 0:pi/100:2*pi;
x=cos(a);
y = sin(x);
fcn=@(z)round(rescale(z,0,intmax('uint16')));
plot(fcn(a),fcn(y))
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:
  1. Have a linear lookup table. This will map all numbers lineraly to the closest integer.
  2. 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)
ans = 1×3
1 2 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.
a=linspace(0,2*pi,200);x=cos(a);y=sin(a);
plot(a,y)
hold on
precision=20;
y2=discretize_values(y,precision);
y2=2*((y2-1)/(precision-1))-1; % map back to non-integers
plot(a,y2)
hold off
function out=discretize_values(in,precision)
% map (-1,1) to 1:precision
if any(in>1) || any(in<-1)
error('data outside expected range')
end
out=1+round((precision-1)*(in+1)/2);
end

Categorías

Más información sobre Geographic Plots en Centro de ayuda y File Exchange.

Productos

Versión

R2021a

Etiquetas

Preguntada:

el 4 de Jul. de 2022

Respondida:

Rik
el 4 de Jul. de 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by