How do convert array x to array z?

4 vues (au cours des 30 derniers jours)
Hamidreza Rafat
Hamidreza Rafat le 28 Août 2018
Modifié(e) : Andrei Bobrov le 28 Août 2018
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

Réponse acceptée

Stephen23
Stephen23 le 28 Août 2018
Modifié(e) : Stephen23 le 28 Août 2018
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

Plus de réponses (1)

Andrei Bobrov
Andrei Bobrov le 28 Août 2018
Modifié(e) : Andrei Bobrov le 28 Août 2018
or just:
[~,~,z] = unique(x,'stable');
or more
[~,z] = ismember(x,[4 8 5]);

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by