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
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 Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!