error using parfor loop - Dimensions of arrays being concatenated are not consistent

1 次查看(过去 30 天)
I got the parallel toolbox to help me with this part of my code (takes weeks to compute). I am not sure where and how best to use the parfor..
I get errors like "Dimensions of arrays being concatenated are not consistent" when i implement it as below
KD=WQ';
hh=[];
HH=[];
gf=size(KD,2);
FF=length(Ped);toc;
%%
for l=1:FF
for i =1:gf
YY{i}=KD{l,i};
r=size(YY{i},1);
% U=zeros(1,length(Arc));
parfor kk=1:r
u=length(YY{i}(kk,:));
uu=YY{i}(kk,:);
Find1=[uu(1:u-1);uu(2:u)];Find1=Find1';
[C,ia,ib] = intersect(Arc,Find1,'rows');
U(ia)=1;
HH=[HH;U];
hh=[hh;length(Find1)-1];
U=zeros(1,length(Arc));
end
end
end

回答(1 个)

Lokesh
Lokesh 2024-11-4
Hello red,
In a 'parfor' loop, temporary variables are cleared at the beginning of each iteration.
In your code, the variable 'U' is cleared at the start of each iteration, leading to issues. To resolve this, ensure that 'U' is initialized before it is used.
Refer to the following code for guidance:
parfor kk = 1:r
u = length(YY{i}(kk, :));
uu = YY{i}(kk, :);
Find1 = [uu(1:u-1); uu(2:u)];
Find1 = Find1';
[C, ia, ib] = intersect(Arc, Find1, 'rows');
U = zeros(1, length(Arc)); % Initialize U here
U(ia) = 1;
HH = [HH; U];
hh = [hh; length(Find1) - 1];
end
For more information, please refer to the documentation on Temporary variables in 'parfor':
  1 个评论
Walter Roberson
Walter Roberson 2024-11-4
Also, I would recommend
uu = YY{i}(kk, :);
Find1 = [uu(1:end-1); uu(2:end)].';
[C, ia, ib] = intersect(Arc, Find1, 'rows');
U = zeros(1, length(Arc)); % Initialize U here
U(ia) = 1;
HH = [HH; U];
hh = [hh; size(Find1,2)];
I have to wonder whether Find1 is being constructed properly; it is currently being constructed as a single row.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by