Quickly converting country names to numbers
5 views (last 30 days)
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?
0 Comments
Accepted Answer
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
end
Or
for c = 1:length(countries)
idx = strcmpi(C{c,1},cellstr(listOfCountriesAsString)); %
listOfCountriesAsCell(idx,1) = C(c,2); % conversion is done here
end
If those assumptions are incorrect, please add more details.
2 Comments
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
0 Comments
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
0 Comments
See Also
Categories
Find more on Data Type Conversion 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!