How to vectorize the following code?

1 view (last 30 days)
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

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 1 Aug 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)

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!