Here's my modified code after integrating advice from the 3 answers below (and if I could I would accept all three of the answers, I only accepted the first because it was the first. There should be some way to accept multiple answers because each one was helpful!):
num_points = 100 * 100 * 75;for i = 1:num_points % zero_pad_length only changes every 3 "s"
zero_pad = repmat(zero_pad_length(:,i), 1, 3)'; offset_trace(1:end-zero_pad,:) = trace_to_offset(zero_pad+1:end,:); results(i) = max(prod(offset_trace)); offset_trace(:) = 0;end
It's 50% faster than it was before. Thanks everyone for your help.
ORIGINAL QUESTION: I've been working at this for a bit, and I'm not really sure where to go from here. The basic structure of what I want to do is (results and offset_trace are preallocated with zeros()):
for i = 1:100 for j = 1:100 for k = 1:75 for s = 1:30 % zero-pad a vector with different lengths of zeros
offset_trace(s,:) = [trace_to_offset(s,1+zero_pad_length(s,i,j,k):end) zeros(1,zero_pad_length(s,i,j,k))]; end % then take the max of its product
results(i,j,k) = max(prod(offset_trace)); end endend
I don't see any way to vectorize the first three for loops (those loop over a 3D grid) because for each point of my grid there's a different pad_length for each of the 30 different rows of trace_to_offset.
I've tried to vectorize the for loop over s with:
str_to_eval = sprintf('[trace_to_offset(%i,1+zero_pad_length(%i,i,j,k):end) zeros(1,zero_pad_length(%i,i,j,k))],', stats');offset_trace = eval(['vertcat(' str_to_eval(1:end-1) ')']);
but it takes exactly as much time to run as the first option. Are there any other solutions that could help me speed up this zero-padding?
Or even better, if it's possible to vectorize the first 3 for loops?
EDIT: For the comments belows and stat should have been "s"! Sorry for the mistakes.
EDIT_2: I've included my modified code that integrates the 3 answers below. Thanks to everyone for your help!
Best Answer