Array Aをソートしそれに合わせArray Bの順序を入れ換える

5 views (last 30 days)
Takashi KOSHIMIZU
Takashi KOSHIMIZU on 5 Jan 2019
Commented: Kazuya on 7 Jan 2019
A = [2 4 5], B = [1.2 0.7 0.1]と与えられた2つのarrayが有ります。
Bは有る関数を元に、Aから生成された値です。
Bを昇順にsortし(結果[0.1 0.7 1.2]となる)、それに合わせarray-A要素順序を並び替える為の手順(code)を教えて下さい。
Aの並びは、[5 4 2]となるMatlabのcoding手法をお伺いしています。

Accepted Answer

Kazuya
Kazuya on 5 Jan 2019
[B,I] = sort(___)
の構文で出力できる I を使えばOKです。
参照:sort 関数のヘルプページ https://jp.mathworks.com/help/matlab/ref/sort.html
A = [2 4 5];
B = [1.2 0.7 0.1];
[~,I] = sort(B);
A(I)
ans =
5 4 2
  2 Comments
Takashi KOSHIMIZU
Takashi KOSHIMIZU on 6 Jan 2019
回答の投稿有難うございます。非常に参考になりました。
~の意味合いも理解させて頂きました。
Kazuya
Kazuya on 7 Jan 2019
よかったです。sortrows 関数だと、行列や table 変数を特定の列のデータをもとに、マルっとソートできるので、より便利かもしれません。

Sign in to comment.

More Answers (1)

madhan ravi
madhan ravi on 5 Jan 2019
sort(A,'descend')
sort(B,'descend')

Categories

Find more on 行列および配列 in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!