Trouble with Indexing and Parfor
1 次查看(过去 30 天)
显示 更早的评论
I've been trying to implement parfor in the following loop for a while but can't seem to find a way to get around the problem that parfor does not allow one to use indexes for variables that are not the loop variable. However given that the index in this case should be able to be sliced I'm wondering if there's a way I could implement parfor or maybe even spmd I'm not seeing.
function tracks_Out = updateAssignedTracks(tracks, centroids, bboxes, assignments)
tracks_Out=tracks;
numAssignedTracks = size(assignments, 1);
for i = 1:numAssignedTracks
trackIdx = assignments(i, 1);
detectionIdx = assignments(i, 2);
centroid = centroids(detectionIdx, :);
bbox = bboxes(detectionIdx, :);
% Correct the estimate of the object's location
% using the new detection.
correct(tracks_Out(trackIdx).kalmanFilter, centroid);
% Replace predicted bounding box with detected
% bounding box.
tracks_Out(trackIdx).bbox = bbox;
% Update track's age.
tracks_Out(trackIdx).age = tracks_Out(trackIdx).age + 1;
% Update visibility.
tracks_Out(trackIdx).totalVisibleCount = ...
tracks_Out(trackIdx).totalVisibleCount + 1;
tracks_Out(trackIdx).consecutiveInvisibleCount = 0;
end
end
I would greatly appreciate any help.
0 个评论
采纳的回答
Ken
2017-5-11
The root cause of the error is that the indexing of the 'assignments' array is not 'fixed'. A requirement of sliced variables in parfor-loops is that the indexing expression must be the same for all occurrences. A few different approaches could solve the issue of different indexing expressions,e.g.,
trackIdx = assignments(i, 1);
detectionIdx = assignments(i, 2);
1. Split the 'assignments' array into two different arrays.
2. Reorganize the 'assignments' array into a cell array of pairs, e.g., trackIdx = assignments{i}(1), detectionIdx = assignments{i}(2)
3. Add a nested for-loop with an if-else statement.
for j=1:2
if mod(j,2) == 1
trackIdx = assignments(i,j)
else
detectionIdx = assignments(i,j)
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!