フィルターのクリア

Replace elements of array with elements of another array without for loop

2 ビュー (過去 30 日間)
Harshil Deepak Prajapati
Harshil Deepak Prajapati 2018 年 3 月 28 日
コメント済み: Guillaume 2018 年 3 月 28 日
A = [1 1 1 3 3 3 4 8 8 10 10 15 15]
B = unique(A)
B = [1 3 4 8 10 15]
I want the output to be [1 1 1 2 2 2 3 4 4 5 5 6 6]
I tried for loop :
for i=1:length(A)
A(A == B(i)) = i;
end
How can I do it without for loop?

採用された回答

Birdman
Birdman 2018 年 3 月 28 日
編集済み: Birdman 2018 年 3 月 28 日
[B,ia,ic] = unique(A)
Third output argument ic will give you what you want.
  1 件のコメント
Guillaume
Guillaume 2018 年 3 月 28 日
So really in this case, since the first two outputs are not needed:
[~, ~, B] = unique(A);

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by