# How to fix index in a parfoor loop?

조회 수: 1 (최근 30일)
Andreu Angles Castillo 2018년 9월 13일
댓글: Andreu Angles Castillo 2018년 9월 18일
I have the following code and get indexing error
parfor dt=0:round(tmax/step)
fprintf(1,'\b\b\b\b\b%05.2f',dt);
for dm=0:dt
A(dm+1,dt+1) = f(dm,dt);
end
end
It does not make sense, for me, because each loop of the first for is stand alone in the variable dt. The size of A has been prefixed.
The error messages on the script are:
-The PARFOOR loop cannot run due to the way varaiable 'A' is used. (On line 1)
-Valid indices for 'A' are resticted in PARFOR loops. (On line 4)
Error when run:
Error: The variable A in a parfor cannot be classified. See Parallel for Loops in MATLAB, "Overview".
##### 댓글 수: 4이전 댓글 2개 표시이전 댓글 2개 숨기기
Stephen23 2018년 9월 13일

@Andreu Angles Castillo: please give the complete error message. This means all of the red text.

Andreu Angles Castillo 2018년 9월 14일
편집: Andreu Angles Castillo 2018년 9월 14일

댓글을 달려면 로그인하십시오.

### 채택된 답변

Titus Edelhofer 2018년 9월 14일
Hi Andreu,
agreed, it looks as if this case could be handled. But it is fairly easy to work around this by separating the inner loop from the parfor loop:
n = round(tmax/step) + 1;
parfor dt=0:round(tmax/step)
fprintf(1,'\b\b\b\b\b%05.2f',dt);
col = zeros(n, 1);
for dm=0:dt
col(dm+1) = f(dm, dt);
end
A(:, dt+1) = col;
end
Titus
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Andreu Angles Castillo 2018년 9월 18일
Thank you very much, it worked for me.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

R2017b

### Community Treasure Hunt

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

Start Hunting!

Translated by