Borrar filtros
Borrar filtros

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

3 visualizaciones (últimos 30 días)
Alexander
Alexander el 12 de Jun. de 2014
Comentada: Geoff Hayes el 12 de Jun. de 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 comentario
Geoff Hayes
Geoff Hayes el 12 de Jun. de 2014
Are you using profile to determine that the majority of time spent is in the table.subsref and table.subasgn methods?

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by