Quickly converting country names to numbers

20 views (last 30 days)
I have 230 country names in my matrix datafile that I would like to convert to numerical three digit ISO codes. I have a table with the conversion names to codes however I am not sure what would be the quickest way to match the names and replace them with numbers. Anyone have any good tips?

Accepted Answer

Kye Taylor
Kye Taylor on 6 Apr 2012
I assume your table is a n-by-2 cell array, where the first column contains cells whose contents are the names of each country and the second column contains cells with the associated ISO codes. Just like the 3-by-2 cell array created with the commands
countries = {'China';'Peru';'Spain'};
codes = randi(10,length(countries),1);
C = [countries,num2cell(codes)];
Then, I also assume that your 230 countries names are really in a cell like the one created with the command
listOfCountriesAsCell = {C{randi(3,230,1),1}}';
Or, if they are in a matrix (does that mean char array?), is your list of countries similar to the array created with the command
listOfCountriesAsString = char(listOfCountriesAsCell};
Either way, you can convert with commands
for c = 1:length(countries)
idx = strcmpi(C{c,1},listOfCountriesAsCell); %
listOfCountriesAsCell(idx,1) = C(c,2); % conversion is done here
for c = 1:length(countries)
idx = strcmpi(C{c,1},cellstr(listOfCountriesAsString)); %
listOfCountriesAsCell(idx,1) = C(c,2); % conversion is done here
If those assumptions are incorrect, please add more details.
Kye Taylor
Kye Taylor on 6 Apr 2012
My pleasure! Thanks for the comments.

Sign in to comment.

More Answers (2)

Gautam Vallabha
Gautam Vallabha on 6 Apr 2012
You can use containers.Map.
First, populate the map (assuming here that your conversion table is a Nx2 cell array):
mymap = containers.Map;
for i=1:numel(conversionTable)
mymap(conversiontable{1}) = conversiontable{2};
Then, run over your list of countries and apply the map
for i=1:numel(mylist)
countrycode(i) = mymap(mylist{i});

Walter Roberson
Walter Roberson on 6 Apr 2012
Use a Perfect Hash if it really has to be done quickly.
Of course constructing the initial hash table might be a bit expensive, which might tempt you to store the hash table into a file, but you need to remember that loading information from disk can be much slower than recalculating the information.
Taking that into account, you might find that in practice (as opposed to in theory) your time is much lower by using something like ismember() to find the array indices and looking up the ISO codes from those indices.
Are you really going to be executing this 230 country look-up millions of times? If not then the time you spend writing and testing and documenting the fastest lookup is going to far exceed the cost of doing the lookups a straight-forward way.
Premature optimization is the root of all evil (or at least most of it) in programming. -- Donald Knuth


Find more on Data Import and Export 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!