How to correct the error “Valid indices for 'variable' are restricted in PARFOR loops”
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Nathaniel Werner
le 8 Juin 2018
Commenté : Walter Roberson
le 13 Juin 2018
I am trying to set up a parfor nested loop in MatLab R2016a as below.
N = size(A,1);
M = size(v,1);
in = zeros(N*M,1);
parfor i=1:N
for j=1:M
k = (i-1)*M-j;
if sqrt(sum((A(i,:)-v(j,:)).^2))<=tol
in(k) = i;
end
end
end
However, I am getting the following error Valid indices for 'in' are restricted in PARFOR loops. Is there some way I can correct this because both arrays A and v are considerably large, over 40,000 rows for A. Thanks very much.
0 commentaires
Réponse acceptée
Walter Roberson
le 9 Juin 2018
Step 1: go back and de-vectorize your in, making it a 2D array again.
Step 2: use temporary variables to process one row at a time, and afterwards
in(i, :) = temporary
It looks to me as if for each row, the columns that are within the tolerance distance should be replaced with the row number, and the ones not within tolerance should stay 0. If you had a logical vector for the row, that would be equivalent to multiplying the vector by the row number.
To get the logical vector you could use pdist2(), or you could perform the equivalent code using bsxfun or the new implicit expansion facilities of R2016b or later.
2 commentaires
Walter Roberson
le 13 Juin 2018
You have
in = zeros(N*M,1);
that is a vector. Instead use
in = zeros(M, N);
Normally I would have said zeros(N, M) given your N*M, but later in the code when you calculated k, you were doing so consistent with zeros(M,N)
"k will count from 1:1:N*M."
Don't do that for parallel work.
in = zeros(N, M);
parfor i=1:N
Ai = A(i,:);
ini = zeros(1,M);
for j=1:M
if sqrt(sum((Ai-v(j,:)).^2))<=tol
ini(j) = i;
end
in(i, ;) = ini;
end
end
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!