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

조회 수: 10 (최근 30일)
red salud
red salud 2019년 6월 20일
댓글: Walter Roberson 2024년 11월 4일 4:25
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일 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
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 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