sort the in plot

20 ビュー (過去 30 日間)
HADIMARGO
HADIMARGO 2021 年 6 月 27 日
編集済み: dpb 2021 年 6 月 27 日
hi.
i have 2 matrix:
a = [ 1.2 6 3]
b = [ 2 4 5]
How do I plot this so that the x and y axes are arranged from less to more?
"x" axes is "a" and "y" axes is "b"
the result for plot should be like this:
x=1.2 , y=2 ------ x=3 , y=5 ----- x=6 , y=4

採用された回答

dpb
dpb 2021 年 6 月 27 日
編集済み: dpb 2021 年 6 月 27 日
Many possible ways...
  1. Sort a, keep index for b ---
[a,ix]=sort(a); % sort a, keep order index, replace a with sorted vector
b=b(ix); % reorder b to match
plot(a,b) % now are in order on abscissa
2. Use array features instead of separate vectors...
ab=sortrows([a(:) b(:)],1); % combined array of a,b by column sorted by column 1 (a)
plot(ab(:,1),ab(:,1)) % plot the two columns against each other
Any number of permuations of the above are also possible to get to same result including not overwriting the original vectors, doing the sorting "on the fly" in the arguments of the call to plot() instead of making explicit variables, etc., etc., etc., ...
  2 件のコメント
HADIMARGO
HADIMARGO 2021 年 6 月 27 日
TNX BROTHER.
[a,ix]=sort(a); % sort a, keep order index, replace a with sorted vector
b=b(ix); % reorder b to match
plot(a,b) % now are in order on abscissa
THIS CODE SOLVED MY PROBLEM.
HOW DID YOU KNOW THIS "[a,ix]=sort(a);"? which book or website?
dpb
dpb 2021 年 6 月 27 日
See
doc sort
read about the possible return values...
It's always the place to start... :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by