how can I sort an array of elements corresponding to another array of elements in decreasing order?

364 ビュー (過去 30 日間)
A = [0.4049 0.1354 0.0914 0.0578 0.0634 0.0494 0.0494 0.0494 0.0494 0.0494]
B = [1 2 3 4 5 6 7 8 9 10]
So 0.4049 corresponds to 1, 0.1354 corresponds to 2, 0.0914 corresponds to 3 etc. I want A to be sorted in decreasing order and B to be in the order that A has come out in. Sorry I don't know if that is clear.
Thanks in advance.

回答 (2 件)

Kirby Fears
Kirby Fears 2016 年 2 月 16 日
編集済み: Kirby Fears 2016 年 2 月 16 日
Try this out:
% sort A in descending order (decreasing A values)
% and keep the sort index in "sortIdx"
[A,sortIdx] = sort(A,'descend');
% sort B using the sorting index
B = B(sortIdx);
Try consulting the documentation for sort if you have other questions along these lines.
Hope this helps.

dpb
dpb 2016 年 2 月 16 日
[A,idx]=sort(A,'descend');
B=B(idx);
doc sort % for details

カテゴリ

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