Slicing structure variable in parfor loop

5 次查看(过去 30 天)
Hi,
I'd like to use a parfor loop instead of a for loop in some code I have written. I currently call a function within a for loop to which I input a structure 'photon'. However, I would only like to input certain entries from the fields within this structure: e.g.
%%loop over all bins
parfor bin = 1:numel(s_uniq)
% determine current step length
s_crnt = s_uniq(bin);
% create photon wight property with photons currently being simulated
photon_crnt.W = photon.W(s_int==s_crnt);
MLint tells me I cannot use 'photon' in this way and that I should slice it. I have tried to do this but continue getting this message.
I'd appreciate your help.
Thanks!

回答(1 个)

Walter Roberson
Walter Roberson 2012-6-19
Each of your loops is writing over the same fieldname in the same structure array entry, photon_crnt.W . You cannot do that unless photon_crnt is a variable that is local to the parfor loop.
If you were to write to photon_crnt(bin).W then that would be allowed by parfor; whether it makes sense for your code is something you will need to decide.
  1 个评论
Roman Hochuli
Roman Hochuli 2012-6-19
Thank you for your answer. I understand what you mean, but the problem arises due to the use of 'photon', not 'photon_crnt'. Moreover, 'photon_crnt' is, as you say, local to the loop. See here:
%% loop over all bins
parfor bin = 1:numel(s_uniq)
% determine current step length
s_crnt = s_uniq(bin);
% create photon wight property with photons currently being simulated
photon_crnt.W = photon.W(s_int==s_crnt);
% if total photon weight currently being simuluated greater than thresh
if any(photon_crnt.W>threshold)
% create photon position property with photons currently being simulated
photon_crnt.pos = photon.pos(s_int==s_crnt,:);
% create photon direction property with photons currently being simulated
photon_crnt.direc = photon.direc(s_int==s_crnt,:);
% absorb over a single step of path length
[photon_crnt, Q] = absorb(map, Q, s, s_int, s_crnt, photon_crnt, direcCosy, direcCosx, SF, threshold,[],[]);
% replace photon data with new data
photon.W(s_int==s_crnt) = photon_crnt.W;
photon.pos(s_int==s_crnt,:) = photon_crnt.pos;
photon.direc(s_int==s_crnt,:) = photon_crnt.direc;
end
end
The alternative referencing you mention would not make sense in this case. Using cell array might be a possibility, but I'm not sure I fully understand how to slice variables yet.
What else could I try?
Thanks!

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by