Trouble with Indexing and Parfor

1 回表示 (過去 30 日間)
Jacob Mevorach
Jacob Mevorach 2017 年 3 月 30 日
コメント済み: Jacob Mevorach 2017 年 5 月 11 日
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 Center および File ExchangeParallel for-Loops (parfor) についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by