"Index exceeds matrix dimensions" error when using parfor
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
0 commentaires
Réponse acceptée
Edric Ellis
le 17 Juin 2015
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 commentaires
Zhiyuan Yang
le 14 Mar 2016
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!
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!