In a section of my code, I intend to do parallel computing for cells: This section is as followed
SpaceNeighbInfo= cell(length(StatesMat),4);
parfor ii=1:length(StatesMat)
[NeigbStates,NeigbStatesIdx,MLNeigb,MRNeigb]=NeigborStates(NJ,StatesMat,StatesMat(ii,:));% a function that has 4 matrix outputs
SpaceNeighbInfo{ii,1}=NeigbStates;
SpaceNeighbInfo{ii,2}=NeigbStatesIdx;
SpaceNeighbInfo{ii,3}=MLNeigb;
SpaceNeighbInfo{ii,4}=MRNeigb;
end
I recieve the following error:
The variable SpaceNeighbInfo in a parfor cannot be
classified.
See Parallel for Loops in MATLAB, "Solve Variable
Classification Issues in parfor-Loops".
But If I commentize the following lines the code can be compiled.
SpaceNeighbInfo{ii,2}=NeigbStatesIdx;
SpaceNeighbInfo{ii,3}=MLNeigb;
SpaceNeighbInfo{ii,4}=MRNeigb;
I am confused, would anyone help me with this?
I thank you very much in advance.
Best regards,
Omid

 채택된 답변

Matt J
Matt J 2022년 5월 7일
편집: Matt J 2022년 5월 7일

1 개 추천

The way to work around this is to write the loop as,
parfor ii=1:length(StatesMat)
[SpaceNeighbInfo{ii,:}]=NeigborStates(NJ,StatesMat,StatesMat(ii,:));% a function that has 4 matrix outputs
end
The reason for the problem is that you are trying to make SpaceNighbInfo a slived variable, but the Fixed-Index Listing restriction on sliced variables is violated. From the documentation:

댓글 수: 3

Thank you very much for your kind answer. I tried what you mentioned but It didn't work for me.
But, the way I could finally resolve it is as the following:
SpaceNeighbInfo = cell(length(StatesMat),4);
SpaceNeighbInfo1= cell(length(StatesMat),1);
SpaceNeighbInfo2= cell(length(StatesMat),1);
SpaceNeighbInfo3= cell(length(StatesMat),1);
SpaceNeighbInfo4= cell(length(StatesMat),1);
parfor ii=1:length(StatesMat)
[NeigbStates,NeigbStatesIdx,MLNeigb,MRNeigb]=NeigborStates(NJ,StatesMat,StatesMat(ii,:));
SpaceNeighbInfo1{ii}=NeigbStates;
SpaceNeighbInfo2{ii}=NeigbStatesIdx;
SpaceNeighbInfo3{ii}=MLNeigb;
SpaceNeighbInfo4{ii}=MRNeigb;
end
SpaceNeighbInfo(:,1)=SpaceNeighbInfo1;
SpaceNeighbInfo(:,2)=SpaceNeighbInfo2;
SpaceNeighbInfo(:,3)=SpaceNeighbInfo3;
SpaceNeighbInfo(:,4)=SpaceNeighbInfo4;
That would indeed fix it, but it's terribly unnecessary, as the following simplified example should demonstrate,
SpaceNeighbInfo=cell(6,4);
parfor ii=1:size(SpaceNeighbInfo,1)
[SpaceNeighbInfo{ii,:}]=NeighborStates(ii) % a function that has 4 matrix outputs
end
SpaceNeighbInfo,
SpaceNeighbInfo =
6×4 cell array
{[1]} {[11]} {[21]} {[31]}
{[2]} {[12]} {[22]} {[32]}
{[3]} {[13]} {[23]} {[33]}
{[4]} {[14]} {[24]} {[34]}
{[5]} {[15]} {[25]} {[35]}
{[6]} {[16]} {[26]} {[36]}
function [varargout]=NeighborStates(ii)
varargout={ii,ii+10,ii+20,ii+30};
end
Thank you very much. Indeed this time it worked nicely! The problem was with my Matlab!
After a restart, your command worked like a charm!
best regards,
Omid

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by