computing vectors with string and numbers (easy question)
Mostrar comentarios más antiguos
Hi evreyone,
I have a vector of names(str vector) and i would like to associate a number to each name
For example evreytime that the name 'Andrea' came up in the first vectror in the second one it gives me back the number 3.
Thank you for your time!
2 comentarios
Stephen23
el 25 de Mzo. de 2021
"I have a vector of 12 names(str vector) and i would like to associate a number to each name (so from 1:12)."
That just sounds like the indices.
Andrea Miceli
el 25 de Mzo. de 2021
Respuesta aceptada
Más respuestas (1)
Paul Hoffrichter
el 25 de Mzo. de 2021
Editada: Paul Hoffrichter
el 25 de Mzo. de 2021
>> S = ["joe" "april" "andrea" "joe" "andrea"]
S =
1×5 string array
"joe" "april" "andrea" "joe" "andrea"
>> SU = unique(S)
SU =
1×3 string array
"andrea" "april" "joe"
>> [~,Locb] = ismember(S, SU)
Locb =
3 2 1 3 1
Every time "joe" came up in S, it shows up as a 3 in Locb.
Every time "andrea" came up in S, it shows up as a 1 in Locb.
Every time "april" came up in S (only 1 time), it shows up as a 2 in Locb.
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!