Unwanted overwriting of duplicates in a PARFOR loop
In MATLAB, the first set of loops below account for duplicates, but the second set of loops (using PARFOR) does not work. They overwrite the previous value. How do we fix this?
For a loop:
for d = 1:length(set),
for k = 1:length(dset),
if strcmp(pset(k),set(d)),
t(h,p) = dset(k);
h = h+1;
end
end
end
PARFOR hinge:
parfor d = 1:length(set),
for k = 1:length(dset),
if strcmp(pset(k),set(d)),
t(d) = dset(k);
end
end
end
Several points ...
-
Typos . Are you sure you should use the pset variable , or did you mean to use dset ? Also, the first set of loops has an undefined p variable . Should the code in the first set of loops read the following ?:
t(h) = dset(k); h = h+1;
-
You don't do the same in every set of loops. Have you tried replacing the line:
t(d) = dset(k);
with the two lines I wrote above?
-
I can't help but notice that each of these sets of loops can be replaced with a vector solution using ISMEMBER . Based on your code above, I believe this should accomplish the same thing:
t = dset(ismember(pset,set));
or, if pset should be dset :
t = dset(ismember(dset,set));
Also, you shouldn't name one of your variables set , as there is a built-in function already called: SET .
a source to share