To pass a variable outside a parfor loop

30 vues (au cours des 30 derniers jours)
George Bashkatov
George Bashkatov le 5 Mai 2021
I have a loop that looks like:
parfor j=1:length(P)
for i=1:(length(A)-1)
%unnecessary calculations
for i=1:(length(A)-1)
end
for k=1:(length(B)-1)
ri(k,l)=len(k); %len is a vector, that has been defined already
sr(1,i)=r(i); %r is a vector, that has been defined already
end
%unnecessary calculations
end
%unnecessary calculations
end
plot(ri,sr); %I know that these two vectors have different dimensions, it's just example to show, that I use variables outside parfor
%loop. By the way, I use the variables in functions "plot" and "meshgrid"
So I tryed to use cell arrays as Walter Roberson recommend:
%in my program (length(A)-1)=40, (length(B)-1) = 10;
ri=cell(10,40); sr=cell(1,40);
parfor j=1:length(P)
for i=1:(length(A)-1)
for k=1:(length(B)-1)
ri{k,l}=len(k);
sr{1,i}=r(i);
end
end
end
ri1=cell2mat(ri);
sr1=cell2mat(sr); %last two lines are my own ideas I don't know good or bad
plot(ri1,sr1);
And matlab writes that because of the way of using variables the parfor loop can't be executed. It writes the same as before changing the code.
The main error is "Unable to classify the variable 'ri' in the body of the parfor-loop". Can you help me, please?

Réponse acceptée

George Bashkatov
George Bashkatov le 6 Mai 2021
Modifié(e) : George Bashkatov le 6 Mai 2021
1) replace (length(A)-1), length(P) and (length(B)-1).
2)make temporary variables lentemp and rtemp
3)add one more index j
4)choose data for plotting
r1=(length(A)-1); r2=length(P); r3=(length(B)-1); %replacing
parfor j=1:r2
for i=1:r1
rtemp=r; %making temporary variables
for k=1:r3
lentemp=len; %making temporary variables
ri(k,l,j)=lentemp(k); %adding index
sr(1,i,j)=rtemp(i); %adding index
end
end
end
plot(ri(:,:,end),sr(:,:,end)); %choosing data

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by