Quickly converting country names to numbers

조회 수: 11 (최근 30일)
AI
AI 2012년 4월 6일
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?

채택된 답변

Kye Taylor
Kye Taylor 2012년 4월 6일
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
AI
AI 2012년 4월 6일
Excellently approached - simple and beautiful. Thank you !
Kye Taylor
Kye Taylor 2012년 4월 6일
My pleasure! Thanks for the comments.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Gautam Vallabha
Gautam Vallabha 2012년 4월 6일
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};
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
Walter Roberson 2012년 4월 6일
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

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by