The PARFOR loop cannot run due to the way the variable is used
2 次查看(过去 30 天)
显示 更早的评论
I do not believe there is a way to further vectorize this code. Please let me know if that is untrue. I would like to parallelize the below code. The goal of the routine is to take the structure array "data", extract values from the fields "goPower" and "ngPower" for each element in the struct, perform some operation, and store the result in a 2-D matrix of size 1501 x number_of_elements_in_struct. (1501x7) for example. I expect MatLab to distribute iterations of the PARFOR loop (corresponding to permi looping index) to workers and have each worker perform the inside loop separately. Since each "subject" is different and each iteration of permi does something different, I don't think I have any dependencies. MatLab still says "The PARFOR loop cannot run due to the way the variable "diff" is used. What am I doing wrong?
parfor permi = 1:npermutations
for subject = 1:numel(data)
% Get indices for subsampled set of Go trials = to # of No-Go trials
idxs = randsample(size(data(subject).goPower,2),size(data(subject).ngPower,2),true);
% Combine trials
allTrials = cat(3,data(subject).goPower(:,idxs,:),data(subject).ngPower);
% Making mapping vector
mapping = [ones(size(allTrials,3)/2,1); ones(size(allTrials,3)/2,1)+1];
% Shuffle the mapping vector
new_map = mapping(randperm(numel(mapping)));
% Calculate mean difference
diff(:,subject) = mean( mean( allTrials(:,new_map==2,:) ,2) - mean( allTrials(:,new_map==1,:) ,2) ,3);
end
% Store values for this iteration
perm_diffs(permi,:) = mean(diff,2);
end
0 个评论
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!