cagri
cagri

Reputation: 71

plot scattered data in order (matlab)

I have some scattered data that i want to plot. but the line of the plot is not following the trend of the scattered data.

A=[1.3476 0.7015 0.2449 0.3402];
B=[0.1 0.2 0.3 0.3];

plot(A,B)
figure
scatter(A,B,'marker','x')

A and B vectors contain only a small number of data points that i have.

as it is seen, the line in plot(A,B) is not following the right order.

I need a line that passes through the points from left to right which appear in the figure when we use scatter command.

thanks!

Upvotes: 0

Views: 1107

Answers (2)

Pursuit
Pursuit

Reputation: 12345

I think that it is easier to see the correctness of the code by performing a parallel re-order of both vectors. I would usually use the following:

[~, ixsSort] = sort(A);
plot(  A(ixsSort), B(ixsSort)  )

(Yes, this is a small stylistic change from the existing response. But the symmetry of the the ore-ordering has helped me avoid mistakes in complex indexing operations.)

Upvotes: 0

Luis Mendo
Luis Mendo

Reputation: 112749

If I understand correctly: sort A and apply the same sorting to B:

[sA, iA] = sort(A);
sB = B(iA);
plot(sA,sB)

Upvotes: 5

Related Questions