How to use a sliced cell array as a reduction variable in parfor?

3 views (last 30 days)
Hi I'm working on a code where I want to use cell arrays as a reduction variable.
You see, I have two matrices called (logErrOuter_1) and (logErrOuter_2) and based on them, I want to iteratively add those cells.
I cant because Matlab errors the valid indices. I wanna use parfor but my reduction variables are cell arrays.
%initialization
for j=1:3
ZerrOuter_1{j} = zeros(j,1);
XerrOuter_1{j} = zeros(j,1);
ZerrOuter_2{j} = zeros(j,1);
XerrOuter_2{j} = zeros(j,1);
end
%Outer syndrome
parfor i = 1:N
%LogErrOuter
logErrOuter_1 = func(...)
logErrOuter_2 = func(...)
%ZerrOuter/XerrOuter
for j=1:3
ZerrOuter_1{j} = ZerrOuter_1{j} + logErrOuter_1(logErrOuter_1(:,2) == j,3);
XerrOuter_1{j} = XerrOuter_1{j} + logErrOuter_1(logErrOuter_1(:,2) == j,4);
ZerrOuter_2{j} = ZerrOuter_2{j} + logErrOuter_2(logErrOuter_1(logErrOuter_1(:,2) == j,1),1);
XerrOuter_2{j} = XerrOuter_2{j} + logErrOuter_2(logErrOuter_1(logErrOuter_1(:,2) == j,1),2);
end
end

Accepted Answer

Matt J
Matt J on 28 Oct 2023
Edited: Matt J on 29 Oct 2023
The update terms like logErrOuter_1(logErrOuter_1(:,2) == j,3) need to be the same size in every iteration i in order to be compatible for accumulated addition. It's not clear why that would be the case.
But assuming you have a way to guarantee the sizes will always be the same, I think you would have to do something like below (which considers only one variable ZerrOuter_1 for simplicity). Then, after the loop, you can split ZerrOuter_1 up with mat2cell, and similarly with the other variables.
%initialization
ZerrOuter_1=0;
%Outer syndrome
parfor i = 1:N
%LogErrOuter
logErrOuter_1 = func(...)
logErrOuter_2 = func(...)
%ZerrOuter
tmp_Zerr1=cell(3,1);
for j=1:3
tmp_Zerr1{j} = logErrOuter_1(logErrOuter_1(:,2) == j,3);
end
ZerrOuter_1 = ZerrOuter_1 + vertcat(tmp_Zerr1{:});
end
  1 Comment
mohadeseh
mohadeseh on 29 Oct 2023
Hi Matt, Yes the update terms have the same size in each iteration i, but different in j. and the problem showed up when I mixed sliced cell arrays with the update terms. your solution worked just as fine. Thanks a lot.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Identification in Help Center and File Exchange

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!