Parallel Processing and Preallocating for Speed
2 次查看(过去 30 天)
显示 更早的评论
So I've been trying to implement parfor in the following for loop
function [tracks_Out, nextId_Out] = createNewTracks(tracks, centroids, bboxes, unassignedDetections, nextId)
centroids = centroids(unassignedDetections, :);
bboxes = bboxes(unassignedDetections, :);
tracks_Out=tracks;
nextId_Out=nextId;
for i = 1:size(centroids, 1)
centroid = centroids(i,:);
bbox = bboxes(i, :);
% Create a Kalman filter object.
kalmanFilter = configureKalmanFilter('ConstantVelocity', ...
centroid, [200, 50], [100, 25], 100);
% Create a new track.
newTrack = struct(...
'id', nextId_Out, ...
'bbox', bbox, ...
'kalmanFilter', kalmanFilter, ...
'age', 1, ...
'totalVisibleCount', 1, ...
'consecutiveInvisibleCount', 0);
% Add it to the array of tracks.
tracks_Out(end + 1) = newTrack;
% Increment the next id.
nextId_Out = nextId_Out + 1;
end
end
The line with tracks_Out(end + 1) gives me the message the variable "tracks_Out appears to change size on every loop iteration, consider preallocating for speed". I think I get that its saying I should have some sort of statement where I say tracks out = zeros(1,1000000) or something to that affect where I pad tracks_Out with a bunch of zeros at the end of it every time but for some reason I'm having trouble understanding how I could do that without leaving a bunch of unnecessary zeros in my tracks variable.
Also, I can't use parfor on this loop because tracks_Out isn't being indexed with the loop variable. I have a feeling that both the parfor problem and the preallocating problem could be solved in the same go with the same solution but I'm having trouble seeing it right now. If anyone could see a way I could do that I'd greatly appreciate it.
2 个评论
Stephen23
2017-3-30
You could read the advice given in the MATLAB documentation (it has examples too):
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!