Rearranging a Vector Back Again

12 ビュー (過去 30 日間)
Hazem El Sankari
Hazem El Sankari 2018 年 12 月 1 日
コメント済み: Hazem El Sankari 2018 年 12 月 1 日
Hello,
I have a vector x:
x =
0 0 1 1 0 1 0 0 1 1
I want arranged having zeros first then ones, so I used this: [x,indices]=sort(x,2)
x =
0 0 0 0 0 1 1 1 1 1
indices =
1 2 5 7 8 3 4 6 9 10
The indices vector is for me to know where each number was displaced from its orignial position to, however, after I finished using the modified x, I would like to rearrange it as its old form again using indices vector, how can I do that?
I used this but it didn't work: sort(indices); x=x(indices)
x =
0 0 0 1 1 0 0 1 1 1

採用された回答

the cyclist
the cyclist 2018 年 12 月 1 日
編集済み: the cyclist 2018 年 12 月 1 日
% Original x
x = [0 0 1 1 0 1 0 0 1 1];
% Sorted x
[x_sorted,indices]=sort(x,2);
% Original x recovered from the sorted one
x_redux(indices) = x_sorted
I renamed the variables so that you would not get confused by which x was which.
  1 件のコメント
Hazem El Sankari
Hazem El Sankari 2018 年 12 月 1 日
Thank you so much sir!

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

その他の回答 (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