Sorting of trigonometric functions
2 views (last 30 days)
Show older comments
Albert Zurita
on 9 Jan 2023
Commented: Albert Zurita
on 11 Jan 2023
I am trying to sort the outputs from sine or cosine function as below. I have a set of angles, for which I compute the sine and then I sort the result of the sine. Is there a way in which I could avoid sorting the sine output and apply a function to the original sorted angle indexes? I presume this is a very stupid question and the answer is NO, but I thought whether there could be a combination in which I could apply a function to ixs1 to get ixs2, so ixs2 = f(ixs1);
ang = rand(1,100)*2*pi;
[~,ixs1] = sort(ang);
[~,ixs2] = sort(sin(ang));
plot(ixs1,'.');hold on;
plot(ixs2);
2 Comments
Mathieu NOE
on 9 Jan 2023
as you expected , the answer is NO because sin is not a monotonic function on 0 - 2pi range
if you had a monotonic function, both indexes would be in linear relationship
ang = rand(1,100)*2*pi;
[~,ixs1] = sort(ang);
% [~,ixs2] = sort(sin(ang)); % answer = NO
[~,ixs2] = sort(1+3*(ang)+0.3*(ang).^2); % answer = YES
plot(ixs1,ixs2,'*');
Accepted Answer
John D'Errico
on 9 Jan 2023
Edited: John D'Errico
on 9 Jan 2023
The simplest way to achieve what you want is to compute the sin, and then do the sort. Anything else you could do would be more complex, even if you could cobble up a solution. And since the sin and sort functions are so easy and fast to use, why is there a problem?
Sigh. CAN you do it? Well, technically yes. Is it any faster, any simpler? Not really. Again, is there really a good reason to not want to just compute the sin?
The point is, to recognize the sin function is periodic over an interval of length 2*pi, AND it is monotonic over the interval [-pi/2,pi/2].
fplot(@sin,[-pi/2,pi/2])
We would effectively need to use the simple identities
sin(x + 2*k*pi) = sin(x)
for any integer k. That gets you into an inteval of length 2*pi. As well, we would need this one:
sin(pi - x) = sin(x)
How can you use that information?
For example, I'll pick a large set of random numbers.
x = rand(1,1000)*50;
They will be spread out over a rather large interval, so any prediction of the sort order, merely from x will be difficult. Not impossible. Anyway, now apply those identities in a careful way.
xhat = pi - mod(x,2*pi);
xhat(xhat > pi/2) = pi - xhat(xhat > pi/2);
xhat(xhat < -pi/2) = -pi - xhat(xhat < -pi/2);
Now, test to see if the sort indices from sort(xhat) are the same as those from sort(sin(x)).
[~,idx1] = sort(xhat);
[~,idx2] = sort(sin(x));
isequal(idx1,idx2)
We can also see that now all elements live in the interval [-pi/2,pi/2], so as long as I used the above identities properly, the sort tags must now behave as desired since sin(x) is monotonic on that interval.
min(xhat) >= -pi/2
max(xhat) <= pi/2
Is there any real gain? I seriously doubt there is much, if any. We could probably test the amount of time needed. I would not be at all surprised if just doing the sort on sin(x) was not just as easy. But whatever floats your boat. If you want to know why what I did works, well, just spend some time thinking about those identities, and how they apply to what I did.
3 Comments
John D'Errico
on 10 Jan 2023
I am sure you don't want to hear me singing like Mick Jagger. ;-) But here it goes "You can't always get what you want..." Yeah, the Stones did it better.
The point is, you are implicitly hoping to approximate pi by the ratio of two integers. And what do we know about pi? Ok, outside of the perusal of some backward school boards, pi is an irrational number, in fact transcendental. Most importantly, irrational. And what do we know about irrational numbers? There is NO pair of integers that repreoduces pi exactly as a ratio of those integers. If you did find one, you would be in line for a Fields medal. And incidentally, also in line for a place on one of those backward boards of education.
At best, you might consider using a better implicit approximation for pi.
rats(pi)
rats(pi,24)
But no matter what, this is still not exact.
You cannot do what you want using integer arithmetic, and have the result be exact.
More Answers (0)
See Also
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!