I need help arranging two vectors

Suppose I have an area vector, composed of two vectors, length and width multiplied together. If I arrange the area from low to high how can I arrange my vector of length and width to match the entries used in the newly arranged area vector. Example: Area(1) = 200, Length(1) = 100, Width(1) = 2, after I sort vector Area, Area(1) moves to Area(3), how do I move Length(1) to Length(3) and also Width(1) to Width(3). Note, my array has 5000 entries.

回答 (2 件)

David Young
David Young 2015 年 3 月 15 日

2 投票

[AreaSorted, idx] = sort(Area);
LengthSorted = Length(idx);
WidthSorted = Width(idx);
Guillaume
Guillaume 2015 年 3 月 15 日

1 投票

Use the second return value of sort which is the original index of the sorted values. Use it to reorder your width and height vector:
width = [5 10 15 20];
height = [14 11 8 5];
area = width .* height;
[area, origidx] = sort(area);
width = width(origidx)
height = height(origidx)

1 件のコメント

Colt Davis
Colt Davis 2015 年 3 月 15 日
You are awesome!

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

カテゴリ

ヘルプ センター および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

質問済み:

2015 年 3 月 15 日

コメント済み:

2015 年 3 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by