error using parfor loop - Dimensions of arrays being concatenated are not consistent
10 ビュー (過去 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
0 件のコメント
回答 (1 件)
Lokesh
2024 年 11 月 4 日 4:14
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
2024 年 11 月 4 日 4:25
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 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!