matlab unable to classify variable in parfor loop

2 次查看(过去 30 天)
function [a_t,b_t,R_t] = beliefs_updating(a_t1,b_t1,R_t1,x_t,y_t,gain)
a_t = zeros(30,1);
b_t = zeros(30,30);
R_t = zeros(2,2,30);
parfor i = 1:30
a_tt1 = a_t1(i);
b_tt1 = b_t1(i,i);
y_tt = y_t(i);
x_tt = [1 x_t(i)]';
phi_t = [a_tt1 b_tt1]';
R_tt = R_t1(:,:,i);
R_tt = R_tt + gain*(x_tt*x_tt' - R_tt);
phi_t = phi_t + gain*inv(R_tt)*x_tt*(y_tt-phi_t'*x_tt)';
R_t(:,:,i) = R_tt;
a_t(i) = phi_t(1);
b_t(i,i) = phi_t(2);
end
Hi all,
I am trying to parallize the above function. The function above takes a 30x1 vectors a_t1,b_t1,x_t,y_t, a 30x30 matrix b_t, a 2x2x30 array R_t to write a new 30x1 vector a_t, 30x30 matrix b_t, and 2x2x30 array R_t.
I want to save time computing running the matrix inversions in parallel, but I get the error message
Error: File: beliefs_updating.m Line: 16 Column: 5
Unable to classify the variable 'b_t' in the body of the parfor-loop.

回答(1 个)

Walter Roberson
Walter Roberson 2021-4-19
b_t(i,i) = phi_t(2);
The parfor loop control variable can only occur in one index location. Store the contents into a vector and store the vector into the diagonal after the parfor.
b_ti(i) = phi_t(2);
...
end %parfor
b_t = diag(b_ti);
  1 个评论
Jacob Thompson
Jacob Thompson 2021-4-19
I just tried that. The parfor loop works now but now instead of the larger function taking .3 seconds to evaluate it now takes a full 6 seconds. It would appear that this operation cannot be efficiently parallelized.
Bummer

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by