Trouble Classifying in a Parfor
2 views (last 30 days)
Show older comments
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!
0 Comments
Answers (1)
Walter Roberson
on 20 Aug 2013
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 Comments
Walter Roberson
on 21 Aug 2013
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.
See Also
Categories
Find more on Parallel for-Loops (parfor) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!