How to retrieve the location of the points plotted ordered

2 次查看(过去 30 天)
I have two vectors x and y which are not sorted. However, when I plot(x,y), it results in the expected plot. Here is an example of the arrays and the plot. As you can see, I would like to have the vectors ordered starting from x(1) = 0, y(1) = 0 and then following the order of the dots as if it was a continuous line. I cannot figure it out how to automatize this process since I have quite a lot arrays/graphs to look at and it is not worth it to do it manually.
x = [1.99802555286103e-05 2.11219844159594e-05 2.28345777469832e-05 2.39763066343323e-05 2.54034677435188e-05 2.59743321871934e-05 2.65451966308679e-05 2.74014932963798e-05 2.85432221837290e-05 2.85432221837290e-05 2.85432221837290e-05 2.85432221837290e-05 2.79723577400544e-05 2.76869255182171e-05 2.65451966308679e-05 2.54034677435188e-05 2.42617388561696e-05 2.28345777469832e-05 2.11219844159594e-05 1.91239588630984e-05 1.71259333102374e-05 1.59842044228882e-05 1.39861788700272e-05 1.19881533171662e-05 9.70469554246784e-06 7.42123776776952e-06 5.70864443674578e-06 3.99605110572207e-06 1.99802555286102e-06 0];
y = [-3.68207566170104e-05 -3.48227310641493e-05 -3.33955699549629e-05 -3.13975444021019e-05 -2.96849510710781e-05 -2.74014932963798e-05 -2.54034677435188e-05 -2.34054421906578e-05 -2.11219844159595e-05 -1.88385266412611e-05 -1.62696366447255e-05 -1.39861788700272e-05 -1.11318566516543e-05 -9.13383109879327e-06 -7.99210221144412e-06 -6.27950888042038e-06 -5.13777999307122e-06 -3.71061888388478e-06 -3.13975444021020e-06 -3.13975444021020e-06 -2.85432221837291e-06 -2.85432221837291e-06 -2.56888999653562e-06 -2.56888999653562e-06 -2.56888999653562e-06 -2.28345777469833e-06 -2.28345777469833e-06 -1.99802555286104e-06 -1.42716110918645e-06 0];

采纳的回答

Star Strider
Star Strider 2020-6-2
The easiest way to do that is to use the flip function (or fliplr or flipud, depending on what you want to do).
Try this:
flipx = flip(x);
flipy = flip(y);
figure
plot(flipx, flipy)
grid
dx = gradient(flipx); % Optional
dy = gradient(flipy); % Optional
figure % Optional
quiver(flipx, flipy, dx,dy) % Optional
grid
The quiver plot just demonstrates that the flip operation does what you want it to do. It is not necesary for the code.

更多回答(1 个)

Fangjun Jiang
Fangjun Jiang 2020-6-2
编辑:Fangjun Jiang 2020-6-2
If you run plot(x,y) instead of plot(x,y,'o'), you will see the correct curve. So your data is already "sorted" as you wanted. I would say that you are lucky that the x, y data is in current order. If x is sorted based on its value, it would be hard to get to the current order.
To illustrate the issue
% x data is in the right order, both curve and dot plot are correct
figure;plot(x,y,'o');
figure;plot(x,y);
% x data is sorted by value, curve plot is incorrect
[xx,index]=sort(x);
yy=y(index);
figure;plot(xx,yy,'*');
figure;plot(xx,yy);
  1 个评论
Kevin Voogd
Kevin Voogd 2020-6-2
Dear Fangjun Jiang, thank you for your time. However, the answer that Star Strider wrote is what I was looking for. I am sorry if my question was not clear enough.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by