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

3 次查看(过去 30 天)
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 个评论
Geoff Hayes
Geoff Hayes 2014-6-12
Are you using profile to determine that the majority of time spent is in the table.subsref and table.subasgn methods?

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by