"Index exceeds matrix dimensions" error when using parfor

4 次查看(过去 30 天)
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!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Parallel Server 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by