How to use parfor?

4 views (last 30 days)
EM geo
EM geo on 25 Mar 2019
Commented: EM geo on 1 Apr 2019
Hi, i'm trying to use par-for in this code, but it matlab gives me this error: "Error: The variable D in a parfor cannot be classified.
See Parallel for Loops in MATLAB, "Overview"."
Anybody can explain me how does parfor works and how to modify this for properly?
clear; close all;
load('data_c2')
tic
% datas= Crosscurv,Elevation,Flowacc,Longcurv,Slope,Rv_mean,Rv_range_interq,Rv_median,Zero
D = [data_c2(:,2),data_c2(:,9),data_c2(:,5),data_c2(:,4),data_c2(:,7),data_c2(:,1),data_c2(:,3),data_c2(:,8)];
%D = Elevation, Zero, Slope, Longcurv, Rv_range_interq, Crosscurv, Flowacc, Rv_median
D(D == -9999) = NaN;
elevMax = max(D(:,1));
slopeMax = max(D(:,3));
longcurvMax = max(D(:,4));
crosscurvMax = max(D(:,6));
flowaccMax = max(D(:,7));
elevMin = min(D(:,1));
slopeMin = min(D(:,3));
longcurvMin = min(D(:,4));
crosscurvMin = min(D(:,6));
flowaccMin = min(D(:,7));
ticBytes(gcp);
PCtime = clock;
Date = strcat(num2str(PCtime(1)),'/',num2str(PCtime(2)),'/',...
num2str(PCtime(3)),'-',num2str(PCtime(4)),':',...
num2str(PCtime(5)));
text = ['Inizio loop ',' - ',Date];
disp(text)
parfor j= (1:length(D))
%max
if D(j,1) == elevMax;
D(j,1) = (elevMax - 1);
end
if D(j,3) == slopeMax;
D(j,3) = (slopeMax - 1);
end
if D(j,4) == longcurvMax;
D(j,4) = (longcurvMax - 1);
end
if D(j,6) == crosscurvMax;
D(j,6) = (crosscurvMax - 1);
end
if D(j,7) == flowaccMax;
D(j,7) = (flowaccMax - 1);
end
%min
if D(j,1) == elevMin;
D(j,1) = (elevMin + 0.1);
end
if D(j,3) == slopeMin;
D(j,3) = (slopeMin + 0.1);
end
if D(j,4) == longcurvMin;
D(j,4) = (longcurvMin + 0.1);
end
if D(j,6) == crosscurvMin;
D(j,6) = (crosscurvMin + 0.1);
end
if D(j,7) == flowaccMin;
D(j,7) = (flowaccMax + 0.1);
end
end
tocBytes(gcp)

Accepted Answer

Patel Mounika
Patel Mounika on 1 Apr 2019
To solve the variable classification issues, make sure that your variables are uniquely classified and meet the respective category requirements. Here the variable D cannot be classified because the indexing of D is not same in all the places which is against the sliced variable category requirement.
Look into the below example.,
parfor i=1:10
b = A(1,i) + A(2,i)
end
Here A is not same in all the places. You can modify the code as follows to make it consistent.
A = [ 1 2 3 4 5 6 7 8 9 10;
10 20 30 40 50 60 70 80 90 100];
B = zeros(1,10);
parfor i=1:10
for n=1:2
B(i) = B(i)+A(n,i)
end
end
Refer to below documentation for more understanding.
Hope this helps.

More Answers (0)

Categories

Find more on Parallel for-Loops (parfor) 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!