valid indices are restricted in parfor loops when iterate every 2 elements

1 view (last 30 days)
Hamed Bolandi
Hamed Bolandi on 13 Feb 2022
Commented: Walter Roberson on 14 Feb 2022
I want to take the max and min of some random matricies every 2 iterations with parfor loop but got the error below:
"valid indices for min1 are restricted in parfor loops"
clc;
clear;
n= 100;
max1 = zeros(1,n);
min1 = zeros(1,n);
intrpVMStress1=0;
iValues = 1:2:n;
parfor idx = 1:numel(iValues)
i= iValues(idx)
x = randi(100,60,60,10);
x_max = max(x(:));
x_min = min(x(:));
max1(i) = x_max;
min1(i) = x_min;
end
max_all_samples = max(max1(:));
min_all_samples = min(min1(:));
max_min_all_samples = [max_all_samples,min_all_samples];

Answers (1)

Walter Roberson
Walter Roberson on 13 Feb 2022
max1(2*idx-1) = x_max;
min1(2*idx-1) = x_min;
and do not use iValue
parfor does want to see computations like that as direct text. You cannot use pre-computed indices. The reason for this is that with the direct text being hardcoded each time, it can do static bounds checks, whereas if you you used computed indices stored in a variable, then it would need to do tracing through all the control levels to prove that the value is always in range and cannot overlap with a different worker.
  3 Comments
Walter Roberson
Walter Roberson on 14 Feb 2022
Note that you initialize min1 to all zero, and you only write in half of the entries, but you min() over all of the entries. You would therefore be taking min() over all of the entries including those zeros. Are you sure that is what you want to do?

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!