Reorganizing integer vector preserving order

1 回表示 (過去 30 日間)
Josue Ortega
Josue Ortega 2023 年 5 月 25 日
回答済み: Jon 2023 年 5 月 25 日
Hi, I have a vector that is a randmperm of n, so it looks like this
x= 2 5 6 13 18 8 14 4 9 3 1 12 16 15 7 11 10 17
Now I have deleteded some entries, based on another different program, in this case 11 and 18, and get the following vector
x'= 2 5 6 13 8 14 4 9 3 1 12 16 15 7 10 17
I want to reorganize x' in such a way that it now becomes a randmperm of 1:size(x'), i.e. that number 12 becomes 11, that number 13 becomes 12, and so on, so that my desired output is
y= 2 5 6 12 8 13 4 9 3 1 11 15 14 7 10 16
Is there an easy way to do this?
  1 件のコメント
Josue Ortega
Josue Ortega 2023 年 5 月 25 日
also, is there a way to make each element of x that is smaller than p, one unit less than its current value?
say something like x(x<p)=p-1?

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

採用された回答

Jon
Jon 2023 年 5 月 25 日
I think this does what you are asking
xp = [2 5 6 13 8 14 4 9 3 1 12 16 15 7 10 17];
[~,idx] = sort(xp);
xnew(idx) = 1:numel(xp)
xnew = 1×16
2 5 6 12 8 13 4 9 3 1 11 15 14 7 10 16

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by