How to save a variable within a parfor loop?
4 ビュー (過去 30 日間)
古いコメントを表示
Hello saviors!
so, I need help: I'm trying to run a parfor loop nested with a forloop, and save the outcome in a matrix array named x_mean that was previously defined , I'm doing this:
muvec= cat(2,fliplr(180:-deltin:inferior),180+deltin:deltin:superior);
delvec= muvec - m(1); %vector array with delta m2
l_vec= length (muvec);
rep=10; %number of replicas
x_mean=zeros (rep,l_vec,n); %matrix array to save the output (x_mean definition)
tspan = y*T;
x_ini=zeros (rep,n); %vector array to save all the generated random x0% (do not pay attention to this,
% this concern to another part of the code)
%here I start de nested parfor:
parfor fu=1:rep
%This dummys variables are a duplicate of the above and are used to decrease run time
dummy_x0=x_ini; % x_ini dummy
dummy_xmean=x_mean; % x_mean dummy ATENTION!
dummy_muvec= muvec; % muvec dummy
dummy_m=m % m dummy
%this parfor calls each x0 in the array x_ini (using its dummy) to be used in the ode45
x0=dummy_x0(fu,:);
%this for loop calls each mu value in the array muvec (using its dummy)
%to be used in the ode 45
for ind=1:l_vec
dummy_m(2)=dummy_muvec(:,ind);
[t, x] = ode45(@odesystem, [0, tspan], x0, [], ...
n, K, N, S, A, T, b, s, p, f, m, w, a, d);
t_uso = find(t>730); % find the indices where t>2 years
x_last = x(t_uso,:); % shows the x values index on t>2 years
dummy_xmean(fu,ind,:)=nanmean(x_last); %mean of x values (density) in the last 2 years (this is the outcome that I want to save in a matrix array)
% I've set de position in wich I want each outcome t be saved
end
x_mean(fu,ind,:)=dummy_xmean % here I try to save each outcome from de loops in a matrix array in its corresponding position
end
But when I run this code, matlab gets me an error "variable x_mean can't be classifiedin a parfor. Anyone can help me with this? thank you very much beforehand
P.S: do not try to run this code, because is not complete it won't run
0 件のコメント
回答 (1 件)
Walter Roberson
2019 年 4 月 4 日
muvec= cat(2,fliplr(180:-deltin:inferior),180+deltin:deltin:superior);
delvec= muvec - m(1); %vector array with delta m2
l_vec= length (muvec);
rep=10; %number of replicas
x_mean=zeros (rep,l_vec,n); %matrix array to save the output (x_mean definition)
tspan = y*T;
x_ini=zeros (rep,n); %vector array to save all the generated random x0% (do not pay attention to this,
% this concern to another part of the code)
%here I start de nested parfor:
parfor fu=1:rep
%This dummys variables are a duplicate of the above and are used to decrease run time
dummy_x0=x_ini; % x_ini dummy
this_xmean = x_mean(fu,:,:); % x_mean dummy ATENTION!
dummy_muvec= muvec; % muvec dummy
dummy_m=m % m dummy
%this parfor calls each x0 in the array x_ini (using its dummy) to be used in the ode45
x0=dummy_x0(fu,:);
%this for loop calls each mu value in the array muvec (using its dummy)
%to be used in the ode 45
for ind=1:l_vec
dummy_m(2)=dummy_muvec(:,ind);
[t, x] = ode45(@odesystem, [0, tspan], x0, [], ...
n, K, N, S, A, T, b, s, p, f, m, w, a, d);
t_uso = find(t>730); % find the indices where t>2 years
x_last = x(t_uso,:); % shows the x values index on t>2 years
this_xmean(ind,:)=nanmean(x_last); %mean of x values (density) in the last 2 years (this is the outcome that I want to save in a matrix array)
% I've set de position in wich I want each outcome t be saved
end
x_mean(fu,:,:)=this_xmean % here I try to save each outcome from de loops in a matrix array in its corresponding position
end
参考
カテゴリ
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!