The variable 'counter' is perhaps intended as a reduction variable, but is actually an uninitialized temporary.
조회 수: 9 (최근 30일)
이전 댓글 표시
I have tried to use parfor to decrease the required time needed for the for loop. The normal for loop was working properly before using parfor but it takes too long to finish the for loop. Then, I have modified the for to parfor to enable using parallel computing. The following error was generated (Error: The variable 'counter' is perhaps intended as a reduction variable, but is actually an uninitialized temporary).
start=8;%%%% elemnent that are not numbers in the file read
var=['_var_',num2str(no_variables),'_'];
a=first;
b=last;
tic
name=first;
counter=0;
for i=a:step:b
filename=[surfacename,num2str(sub),var,num2str(i),'.raw'];
fullname=fullfile(Dir,filename);
fid = fopen(fullname,'rb'); % rb = read binary
NN=N*10;
data2 = fread(fid,NN,'single');
fclose(fid);
start=8;
data2=data2(start:length(data2));
len=length(data2)/no_variables;
counter=counter+1;
if opt==1 ||opt==6;
data(:,counter)=data2(1:len);
elseif opt==2;
data(:,counter)=data2(len+1:2*len);
elseif opt==3;
data(:,counter)=data2(2*len+1:3*len);
elseif opt==4;
data(:,counter)=data2(3*len+1:4*len);
elseif opt==5;
data(:,counter)=data2(4*len+1:5*len);
elseif opt==7;
F = scatteredInterpolant(x,y,z,data2(1:len),'linear','linear');
data_int=F(xq,yq,zq);
data_int_2= reshape(data_int,[],1);
data(:,counter)=data_int_2;
end
if mod(i,500) == 0
fprintf('flie read %d...\n',i);
tt=toc/60
end
name=[name,i];
end
tt=toc/60
댓글 수: 0
채택된 답변
Rik
2022년 4월 8일
Your loop depends on the previous iterations. You should calculate the value of the counter variable inside the loop. Otherwise you cannot make this parallel.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!