How do convert array x to array z?

1 回表示 (過去 30 日間)
Hamidreza Rafat
Hamidreza Rafat 2018 年 8 月 28 日
編集済み: Andrei Bobrov 2018 年 8 月 28 日
Hi, How do convert array x to array z ?
x = [4 4 8 8 8 5 5 4 8 8 4]
Z = [1 1 2 2 2 3 3 1 2 2 1]
4 -> 1
8 -> 2
5 -> 3

採用された回答

Stephen23
Stephen23 2018 年 8 月 28 日
編集済み: Stephen23 2018 年 8 月 28 日
If you want to convert the first value, whatever it may be, to 1, the second to 2, etc:
>> x = [4 4 8 8 8 5 5 4 8 8 4];
>> [~,ia,ic] = unique(x,'first');
>> [~,ia] = sort(ia);
>> z = ia(ic)
z =
1 1 2 2 2 3 3 1 2 2 1
  1 件のコメント
Hamidreza Rafat
Hamidreza Rafat 2018 年 8 月 28 日
Tanx

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

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2018 年 8 月 28 日
編集済み: Andrei Bobrov 2018 年 8 月 28 日
or just:
[~,~,z] = unique(x,'stable');
or more
[~,z] = ismember(x,[4 8 5]);

カテゴリ

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