How to vectorize the following code?

1 visualizzazione (ultimi 30 giorni)
function kod = sehir2kod(sehirName)
switch strtrim(sehirName)
case 'Adana'
kod = 01;
case 'Adıyaman'
kod = 02;
case 'Afyon'
kod = 03;
case 'Ağrı',
kod = 04;
case 'Amasya'
kod = 05;
end
sehirName = {'Ağrı';'Ağrı';'Ağrı';'Amasya';'Amasya';'Amasya';'Amasya';'Adana'};
kod = zeros(length(sehirName),1);
for idx = 1:length(sehirName)
kod(idx) = sehir2kod(char(sehirName(idx)));
end
Any help is appreciated. Thanks

Risposta accettata

Azzi Abdelmalek
Azzi Abdelmalek il 1 Ago 2016
v={'Adana','Adıyaman','Afyon','Ağrı','Amasya'}
sehirName = {'Ağrı';'Ağrı';'Ağrı';'Amasya';'Amasya';'Amasya';'Amasya';'Adana'};
[~,kod]=ismember(sehirName,v)

Più risposte (0)

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by