How do I sort data based on another variable?

32 ビュー (過去 30 日間)
Shayma Al Ali
Shayma Al Ali 2021 年 9 月 16 日
コメント済み: John D'Errico 2021 年 9 月 16 日
One variable, b_dp, is dependent on another variable, b_lat which is the latitude the data b_dp was taken at. I know they are not the same size at the moment but it's because I didn't update the code yet. I want to compare b_dp with sst but I currently cannot because sst variable is ordered by latitude (so -90 to 90 for example) while b_dp is ordered based on the time the sample was taken. I want to order b_lat from smallest to largest so it's similar to the sst data. However, how do I then order b_dp in a similar way? I will attach the data here so you can get an idea. I don't have any sample code because I don't even know where to begin.
  1 件のコメント
John D'Errico
John D'Errico 2021 年 9 月 16 日
If they are not the same size, then a sort of one based on the other is meaningless. So until you resolve that problem and clean up your data, then you can do nothing.
Once you have done so, then use the answer Adam gave you.

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

回答 (1 件)

Adam Danz
Adam Danz 2021 年 9 月 16 日
If and only if two vectors have the same number of elements, you can sort vector b according to the sorted vector a using,
[aSorted, aIdx] = sort(a);
bSorted = b(aIdx);

カテゴリ

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