"Index exceeds matrix dimensions" error when using parfor

조회 수: 2 (최근 30일)
Siva
Siva 2015년 6월 17일
댓글: Walter Roberson 2016년 3월 15일
I am using Matlab R2013a 64-bit. I am getting an "Index exceeds matrix dimensions" error when using parfor. The following is a minimal code that produces this error.
matlabpool open 4
nbig = 200;
nsmall = 100;
x = rand(3,nsmall);
parfor n = 1:nbig
if (n <= nsmall)
a = x(:,n);
else
a = zeros(3,1);
end
end
matlabpool close
I am wondering why this happens. Thanks.

채택된 답변

Edric Ellis
Edric Ellis 2015년 6월 17일
parfor is treating x as a sliced variable because of the form of indexing you're using. Once x is determined to be "sliced", the parfor machinery sends slices of x to the workers without knowledge of what they're going to do. Hence it tries to send slices of x that do not exist, and you get the error.
You could make this loop work by forcing parfor to send the whole value of x to each worker by including an operation inside the loop that is not in the sliced form. Here's one way:
parfor n = 1:nbig
if n <= size(x,1) % unsliced access to "x" forces "x" to be "broadcast"
a = x(:, n);
else
a = zeros(3, 1);
end
end
  댓글 수: 2
Zhiyuan Yang
Zhiyuan Yang 2016년 3월 14일
Hi. I got the similar problem. When I run an enumeration combination problem with nchoosek(1:24,n). it works fine. But when I work for nchoosek(1:27,n) n starts from 1 and it terminates at 9 and the program halted. Erroring: Index exceeds matrix dimensions. Do you what is wrong? Thank you very much!
Walter Roberson
Walter Roberson 2016년 3월 15일
We will need to see your code

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by