Quickly converting country names to numbers
Show older comments
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
More Answers (2)
Gautam Vallabha
on 6 Apr 2012
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};
end
Then, run over your list of countries and apply the map
for i=1:numel(mylist)
countrycode(i) = mymap(mylist{i});
end
Walter Roberson
on 6 Apr 2012
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
Categories
Find more on Characters and Strings 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!