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.

采纳的回答

Ken
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
  1 个评论
Jacob Mevorach
Jacob Mevorach 2017-5-11
Thanks Ken! I was able to solve the problem using a version of number 1. If anyone wants details feel free to contact me.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by