Hi Hayley,
Vectorizing spline functions, especially under various conditions, might not always be straightforward. It might be worth exploring alternative approaches for optimization or vectorization in your case.
For instance, if we maintain the loop “for c = 1:height(Var)”, we can optimize by setting “xValues” statically as “[1,2,3]” since they don't change. This not only saves memory but also simplifies the calculation of “yValues” for cases where “GroupLineNum{c} >= 3”.
When it comes to calculating splines, doing so conditionally—only when necessary—and assigning missing values otherwise can streamline the process. Additionally, I noticed the use of "dummy_tables" for padding; consider adding them back only if they serve a critical purpose outside the demonstrated scope.
output = array2table(zeros(height(Var), 6), 'VariableNames', {'spx', 'pcx', 'mkx', 'spxx', 'pcxx', 'mkxx'});
% Constants for x values
xValues = [1, 2, 3];
% Loop through each set of points
for c = 1:height(Var)
if GroupLineNum{c} < 3
% Assign missing values if condition is met
output{c, :} = missing;
else
% Extract y values for current set, considering conditions
yValues = Var(max(1, c-2):c) .* (GroupLineNum{max(1, c-2):c} >= 3);
% Skips rows when previous 2 data points doesn’t exist
if numel(yValues) < 3
continue;
end
% Spline and its derivatives
sp = spline(xValues, yValues);
pc = pchip(xValues, yValues);
mk = makima(xValues, yValues);
spx = fnder(sp, 1);
pcx = fnder(pc, 1);
mkx = fnder(mk, 1);
spxx = fnder(sp, 2);
pcxx = fnder(pc, 2);
mkxx = fnder(mk, 2);
% Evaluate derivatives at x = 3
output{c, 'spx'} = ppval(spx, 3);
output{c, 'pcx'} = ppval(pcx, 3);
output{c, 'mkx'} = ppval(mkx, 3);
output{c, 'spxx'} = ppval(spxx, 3);
output{c, 'pcxx'} = ppval(pcxx, 3);
output{c, 'mkxx'} = ppval(mkxx, 3);
end
end
This approach should offer a more streamlined and efficient way to handle your data, especially considering the volume you mentioned (2M+ data points). If performance is still a concern, leveraging “parfor” for parallel execution might further enhance the process.
I hope this helps!