Trouble with Indexing and Parfor

3 views (last 30 days)
Jacob Mevorach
Jacob Mevorach on 30 Mar 2017
Commented: Jacob Mevorach on 11 May 2017
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.

Accepted Answer

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

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!