biji buji
biji buji

Reputation: 465

MATLAB plot with sorted order

I have two vectors in MATLAB, say:

x = [1 20 3 7 10]  

and

y = [2 51 1 9 18]  

How can I plot y vs K where x has sorted value order (1 3 7 10 20) with their respective y values like the following?

x = [1 3 7 10 20]
y = [2 1 9 18 51]

Upvotes: 2

Views: 6860

Answers (2)

Dan
Dan

Reputation: 45752

XY = sortrows([x ; y]');

plot(XY(:,1), XY(:,2));

Concatenate the matrices, transpose them and then you can use sortrows to order by X

Upvotes: 1

devan
devan

Reputation: 1653

Call sort with a second output argument.

x = [1 20 3 7 10]  
y = [2 51 1 9 18]  

[xsorted, I] = sort(x)
ysorted = y(I)

Upvotes: 6

Related Questions