"Bek Abdik" <beknazar@unist.ac.kr> writes:
> I am having problems with the following loop, since it is taking too
> much time. Hence, I would like to use parallel processing,
> specifically parfor function.
>
> Can anyone, please, help me to convert the following 'far' loop into 'parfor'?
>
> P = numel(scaleX); % quite BIG number
> sz = P;
> start = 1;
> sqrL = 10; % sqr len
> e = 200;
> A = false(sz, sz);
>
> parfor m = sz-sqrL/2:(-1)*sqrL:start
> for n = M(m):-sqrL:1
> temp = [scaleX(m), scaleY(m); scaleX(n), scaleY(n)];
> d = pdist(temp, 'euclidean');
> if d < e
> A(m, n) = 1;
> end
> end
> end
As the code analyzer message informs you, the range of a PARFOR loop
must be consecutive integers. There's more about the restriction here:
<http://www.mathworks.com/help/distcomp/parfor.html>
The other problem you have is that the form of indexing you're using
with 'A' still wouldn't work. This is because of the form of your inner
FOR loop, as described here:
<http://www.mathworks.com/help/distcomp/nesting-and-flow-in-parfor-loops.html#bq__cs7-9>
To work around that, I would normally create a temporary vector and then
assign a whole row of A, something like this:
....
mv = sz-sqrL/2:(-1)*sqrL:start;
parfor mi = 1:numel(mv)
m = mv(mi);
Arow = false(1, sz);
for n = 100:-sqrL:1
temp = [scaleX(m), scaleY(m); scaleX(n), scaleY(n)];
d = pdist(temp, 'euclidean');
if d < e
Arow(n) = true;
end
end
A(mi, :) = Arow;
end
Obviously, you need to map back from the 'A' that has been created since
your original loop is only accessing some of the rows of A.
Cheers,
Edric.