Parallel Processing and Preallocating for Speed
3 ビュー (過去 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 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!