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!