Effacer les filtres
Effacer les filtres

How can I speed up a function operating on a table?

3 vues (au cours des 30 derniers jours)
Alexander
Alexander le 12 Juin 2014
Commenté : Geoff Hayes le 12 Juin 2014
I have a table that is roughly 100x20. I have a function that takes a set of rows from this table and returns the same set of rows with some of the variables (columns) modified. For the sake of clarity, I use dot indexing within the function to access the variables I'm modifying. I call this function many times, and the majority of the time spent on the function is spent in table.subsref and table.subasgn. How can I speed up my function?
Below is a generic function with similar behavior.
load patients
patients = table(Age,Gender,Height,Weight,Smoker,...
'RowNames',LastName);
patients.Factor = zeros(size(patients,1),1);
rows = strcmp(patients.Gender,'Male');
patients(rows,:) = TableFcn(patients(rows,:),10);
rows = strcmp(patients.Gender,'Female');
patients(rows,:) = TableFcn(patients(rows,:),7);
function T = TableFcn(T,modulus)
if nnz(double(T.Smoker)) >= 5
TotalHeight = sum(T.Height);
Factor = mod(100*T.Height/TotalHeight,modulus);
else
TotalWeight = sum(T.Weight);
Factor = mod(100*T.Weight/TotalWeight,modulus);
end
Numbers = repmat((1:2:9).',20,1)/10;
for ii = 1:size(T,1)
if T.Age(ii) < 30
T.Factor(ii) = 18*Factor(ii);
elseif T.Age(ii) < 40
sum(Numbers(1:T.Age(ii)))
T.Factor(ii) = Factor(ii)*sum(Numbers(1:T.Age));
else
T.Factor(ii) = NaN;
end
end
end
  1 commentaire
Geoff Hayes
Geoff Hayes le 12 Juin 2014
Are you using profile to determine that the majority of time spent is in the table.subsref and table.subasgn methods?

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by