parfor variable classification issue

1 view (last 30 days)
I had the code as such (note the size of the map array is (1001,1001,3):
map = zeros(i1max - i1min + 1,j1max - j1min + 1,3);
parfor (i1 = i1min:i1max)
for j1 = j1min:j1max
[a,b] = some_function(1,i1,j1);
%some work
map(i1,j1,1)=r1;
map(i1,j1,2)=phi1;
map(i1,j1,3)=alpha;
end
end
But matlab complained that Parfor cannot run due to the way the variable map is being used. So after reading some online documentation, i changed it to :
map = zeros(i1max - i1min + 1,j1max - j1min + 1,3);
parfor (i1 = i1min:i1max)
map_temp = zeros(j1max - j1min + 1,3);
for j1 = j1min:j1max
[a,b] = some_function(1,i1,j1);
%some work not using i1 or j1 variables but a,b
jdx = j1 - j1min + 1;
map_temp(jdx,1)=r1;
map_temp(jdx,2)=phi1;
map_temp(jdx,3)=alpha;
end
idx = i1 - i1min + 1;
map(idx,:,:) = map_temp;
end
But that does not work either because apparently as matlab complains "Valid indices are restricted", it has something to do with the idx variable. Why? And how to make this work?

Accepted Answer

Matt J
Matt J on 25 May 2021
Edited: Matt J on 25 May 2021
map is a sliced variable, so it can only be indexed using the loop variable.
map = zeros(i1max - i1min + 1,j1max - j1min + 1,3);
parfor (idx = 1:i1max-i1min+1)
i1=idx+i1min-1;
map_temp = zeros(j1max - j1min + 1,3);
for j1 = j1min:j1max
[a,b] = some_function(1,i1,j1);
%some work not using i1 or j1 variables but a,b
jdx = j1 - j1min + 1;
map_temp(jdx,1)=r1;
map_temp(jdx,2)=phi1;
map_temp(jdx,3)=alpha;
end
map(idx,:,:) = map_temp;
end
  1 Comment
Raymond Norris
Raymond Norris on 25 May 2021
I read the posting, but went back to it later without refreshing the page first. Matt's got the idea.

Sign in to comment.

More Answers (1)

Raymond Norris
Raymond Norris on 25 May 2021
As a side note, the difference of min and max could be 1000, but there are infinity cases where j1 will start outside the boundaries of map. For instance,
j1min = 200000;
j1max = 201000;
In this case, j1 = 200000, which would far exceed the allocation of map (1001x1001x3). This would still work, but it's probably not what you want.
With that said, try the following. You're using i1 as a parameter for some_function but you really need i1 to slice map (which is what you're defining idx for), so let's swap it. Assign idx before parfor as such
idx = i1min:i1max;
and then use the loop variable, i1, to pass it to some_function. Now use i1 to index into map at the bottom of the loop.
map = zeros(i1max - i1min + 1,j1max - j1min + 1,3);
idx = i1min:i1max; % used for some_function
parfor (i1 = i1min:i1max - i1min + 1)
map_temp = zeros(j1max - j1min + 1,3);
for j1 = j1min:j1max
[a,b] = some_function(1,idx(i1),j1);
%some work not using i1 or j1 variables but a,b
jdx = j1 - j1min + 1;
map_temp(jdx,1)=r1;
map_temp(jdx,2)=phi1;
map_temp(jdx,3)=alpha;
end
% idx = i1 - i1min + 1; % not needed
map(i1,:,:) = map_temp; % use i1, not idx
end

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!