Add/change value in a table based on a categorical array in the same table
Ältere Kommentare anzeigen
Hello everybody,
I have a table called testData that contains a categorical column with element names. Another table called List assignes to the element a specific numerical value. The aim of my code is to add an additional column to the testData and add the numeric values assigned in the table List.
% create List each element has a numeric value
ionName = categorical(["Co", "Cr", "Ga"]');
ionNumeric = [ 40, 30, 20]';
list = table(ionName,ionNumeric);
% create Test Data table
ionNameTest = categorical(["Co", "Cr", "Ga", "Co", "Cr", "Ga", "Co", "Cr", "Ga", "Co", "Cr", "Ga","Co", "Cr", "Ga"]');
testData = table(ionNameTest);
% my code:
% add the column
h = height(testData);
testNumeric = zeros(h,1);
testData = addvars(testData, testNumeric);
% go through each cell of list -> then trough each element in list and
% compare the names -> change the value
for i = 1:height(testData)
for j = 1:height(list)
if list.ionName(j) == testData.ionNameTest(i)
testData.testNumeric(i) = list.ionNumeric(j);
end
end
end
In real life my table has more than 4 mio entities - so my code takes over an hour to run - is there any faster way ? or less complex way?
Thank you for your help!
Martina
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Time-Frequency Analysis finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!