Trouble Classifying in a Parfor

3 次查看(过去 30 天)
Diego
Diego 2013-8-19
Hello,
Im having trouble fixing the "cannot be classified error" in my parfor loop:
n = 1;
post = zeros(100,100,100)
wvals = 0:.01:1;
numerator = zeros(length(post(:,:,n)));
currW = cell(length(wvals), length(wvals));
parfor indeX = 1:length(wvals)
for indeY = 1:length(wvals)
if n == 1
prior = ones(length(wvals), length(wvals)); % initially uniform prior
currW{indeX, indeY} = [wvals(indeX) wvals(indeY)]';
else
prior = posterior(:,:,n-1); % previous posterior
prevY = yChanOutput(n-1);
% W_t(u) = S_{y-1}(W_{t-1}(u))
currW{indeX, indeY} = map(prevW{indeX, indeY}, S_opt{prevY+1}, wvals, resolution);
end
currX = encode(currW{indeX, indeY});
numerator(indeX, indeY) = prior(indeX, indeY)*likelihood(currChanOutputY, currX, errorProb);
end
end
posterior(:,:,n) = numerator/(sum(numerator(:)*(1/resolution))); %/( sum(numerator*(1/resolution)) );
prevW = currW;
There is an outer loop that is not shown, that handles moving n around. I get an error saying currW cannot be classified, though it looks like a "sliced output" variable. Any help would be great thanks!

回答(1 个)

Walter Roberson
Walter Roberson 2013-8-20
It is not clear at the moment that currW{indeX, indeY} is ever used outside the parfor(). If it is not, then use an unindexed name (writing over it each time.)
If you do need currW after the parfor() then try constructing a local cell array currY inside the parfor, and at the end of the loop store that in a cell vector indexed by indeX. If you need to you can break it out from a vector of vectors to a 2D array after the parfor.
  5 个评论
Walter Roberson
Walter Roberson 2013-8-21
Here is a partial conversion. I suspect it would be a bit more efficient. See my comments below the code:
n = 1;
post = zeros(100,100,100)
wvals = 0:.01:1;
Nw = length(wvals);
numerator = zeros(length(post(:,:,n)));
currW = cell(Nw, 1);
parfor indeX = 1:Nw
currWX = cells(Nw,1);
prevWX = prevW{indeX};
for indeY = 1:Nw
if n == 1
prior = ones(Nw, Nw); % initially uniform prior
currWX{indeY} = [wvals(indeX) wvals(indeY)]';
else
prior = posterior(:,:,n-1); % previous posterior
prevY = yChanOutput(n-1);
% W_t(u) = S_{y-1}(W_{t-1}(u))
currWX{indeY} = map(prevWX{indeY}, S_opt{prevY+1}, wvals, resolution);
end
currX = encode(currWX{indeY});
numerator(indeX, indeY) = prior(indeX, indeY)*likelihood(currChanOutputY, currX, errorProb);
end
currW{indeX} = currWX;
end
posterior(:,:,n) = numerator/(sum(numerator(:)*(1/resolution))); %/( sum(numerator*(1/resolution)) );
prevW = currW;
I fixed the indentation, which was important for understanding that the prevW is not to be updated until after the parfor loop completes. You do not initialize prevW outside the loop, so the assumption I must make is that n is not always set to 1, but it will be set to 1 the first time this code is executed. I probably would have programmed this slightly differently, with a completely different code path for n==1 or not, as it is a waste of time to keep testing n for each indeX and indeY combination.
Diego
Diego 2013-8-21
编辑:Diego 2013-8-21
Hello! Thank you for your help. It is very interesting to see what you cleverly did with cells! I have incorporated your changes and I get it to run for n=1. However, when the outer loop moves n=2, I get the following error:
Error using parallel_function (line 589)
Cell contents reference from a non-cell array
object.
Error stack:
myFunction>(parfor body) at 134
Error in myFunction(line 123)
parfor indeX = 1:lenWvals
Line 134 is:
currWX{indeY} = map(prevWX{indeY}, S_opt{prevY+1}, wvals, resolution);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by