How to resolve indexing the sliced variable error in the case of for nested with parfor?
Show older comments
I have following use case:
Nth = 102;
p = 0.0000001:0.1:0.99999;
NthArray = (Nth - 100 > 0)*Nth + (Nth - 100 <=0)*1:1:Nth+100;
III = zeros( length(p), length(NthArray));
parfor ip = 1:numel(p)
for inth = 1:numel(NthArray)
III(ip, inth) = g( p(ip), ntharray(inth));
end
end
However, upon running it I get following error:
Error: When indexing the sliced variable 'III', the range of the for-loop variable 'inth' must be a row vector of positive constant numbers or variables. For more information, see Parallel
for Loops in MATLAB, "Nested for-Loops with Sliced Variables".
How should I resolve this error?
Thanks.
Answers (1)
The problem here is that the inner loop bounds must be a constant - basically this means that you must compute it outside the parfor loop, like so:
Nth = 102;
p = 0.0000001:0.1:0.99999;
NthArray = (Nth - 100 > 0)*Nth + (Nth - 100 <=0)*1:1:Nth+100;
III = zeros( length(p), length(NthArray));
% Must calculate inner loop bounds outside the PARFOR loop:
n_Nth = numel(NthArray);
g = @plus; % Dummy definition of "g"
parfor ip = 1:numel(p)
for inth = 1:n_Nth
III(ip, inth) = g( p(ip), NthArray(inth));
end
end
disp('Success!')
6 Comments
Qitr
on 10 Nov 2023
Moved: Edric Ellis
on 10 Nov 2023
I have the same problem in the following code. Even though, both indexes are constant I obtaine error:
When indexing the sliced variable 'RintE_mat', the range of the for-loop variable 'itm2' must
be a row vector of positive constant numbers or variables. For more information, see Parallel
for Loops in MATLAB, "Nested for-Loops with Sliced Variables".
What am I doing wrong?
parfor itm=1:2*NmaxAMM
gz_real_coeff = squeeze(gzvec_real(itm,:));
gz_real_interp(itm,:) = sqrt(interp1(grid_vec,gz_real_coeff.^2,fvec,'spline','extrap'));
for itm2=1:2*NmaxAMM
RintE_coeff = squeeze(RintE_mat0(itm,itm2,:));
RintH_coeff = squeeze(RintH_mat0(itm,itm2,:));
RintE_interp_real = interp1(grid_vec,real(RintE_coeff),fvec,'spline','extrap');
RintE_interp_imag = interp1(grid_vec,imag(RintE_coeff),fvec,'spline','extrap');
RintE_mat(itm,itm2,:) = RintE_interp_real + 1i*RintE_interp_imag;
RintH_interp_real = interp1(grid_vec,real(RintH_coeff),fvec,'spline','extrap');
RintH_interp_imag = interp1(grid_vec,imag(RintH_coeff),fvec,'spline','extrap');
RintH_mat(itm,itm2,:) = RintH_interp_real+1i*RintH_interp_imag;
end
end
Edric Ellis
on 10 Nov 2023
Edited: Edric Ellis
on 10 Nov 2023
Unfortunately this is a limitation of the parfor implementation. It cannot handle the expression you have there for the nested for-loop bounds (and the error is rather confusing I admit). You simply need to compute that value outside the parfor loop, like so:
limit = 2*NmaxAMM;
parfor itm=1:limit
% then
for itm2 = 1:limit
end
end
Qitr
on 10 Nov 2023
Have you tested that? because I tried it and it does not improved the situation. Finally, I've inserted the inner loop inside the function and it works, but the error is really confusing :(
Edric Ellis
on 10 Nov 2023
I didn't try it with your code because your code is not executable in that form, so I tried it with a simpler case. Let me look again to see if there are further problems.
Here's an executable version of my previous comment. Hopefully this includes the salient points from your code, and this does work.
limit = 2*3;
out = zeros(limit, limit, 2);
parfor i = 1:limit
for j = 1:limit
out(i,j,:) = [i j];
end
end
disp(out)
Qitr
on 11 Nov 2023
I cannot say anything more than thank you very much!
Categories
Find more on Loops and Conditional Statements 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!