Parallel Processing and Preallocating for Speed

조회 수: 4 (최근 30일)
Jacob Mevorach
Jacob Mevorach 2017년 3월 30일
댓글: Jacob Mevorach 2017년 4월 17일
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
Stephen23 2017년 3월 30일
You could read the advice given in the MATLAB documentation (it has examples too):
Jacob Mevorach
Jacob Mevorach 2017년 4월 17일
Thank you Steven! This material ended up helping me out a lot.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Processing and Computer Vision에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by