Before you start to vectorize the code, did you pre-allocate the output before the loops? This is essential:
L = (length(time)-1);
x = zeros(numPT, L);
y = zeros(numPT, L);
c = zeros(numPT, L);
for q = 1:numPT
for h=1:L
x(q,h) = SortDate{q+numPT*(h-1),6};
y(q,h) = SortDate{q+numPT*(h-1),7};
c(q,h) = SortDate{q+numPT*(h-1),5};
end
end
Now compare the timings. Then in the next step remove the inner loop:
L = (length(time)-1);
x = zeros(numPT, L);
y = zeros(numPT, L);
c = zeros(numPT, L);
for q = 1:numPT
x(q, :) = [SortDate{q+numPT*(0:L-1), 6}];
y(q, :) = [SortDate{q+numPT*(0:L-1), 7}];
c(q, :) = [SortDate{q+numPT*(0:L-1), 5}];
end
And now the outer loop - and then a pre-allocation is not required:
L = (length(time)-1);
index = (1:numPT).' + (0:numPT:numPT*(L-1)));
x = reshape(cell2mat(SortDate(index, 6)), size(index));
y = reshape(cell2mat(SortDate(index, 7)), size(index));
c = reshape(cell2mat(SortDate(index, 5)), size(index));
Do you see how the indices are moved from the for loop into the vector indices?
If you have an older Matlab version, you need bsxfun instead of the modern auto-expanding:
index = bsxfun(@plus, (1:numPT).', (0:numPT:numPT*(L-1)));
If this is still the bottleneck of you code, try to use the faster FEX: Cell2Vec, which seems to have a better memory management. Perhaps it is faster to convert the complete SortDate array at once: L = (length(time)-1);
index = (1:numPT).' + (0:numPT:numPT*(L-1)));
siz = size(index);
M = Cell2Vec(SortDate(:, 5:7));
x = reshape(M(index, 2), siz);
y = reshape(M(index, 3), siz);
z = reshape(M(index, 1), siz);
Best Answer