Problem in using parfor for matrix

2 vues (au cours des 30 derniers jours)
Aep
Aep le 6 Mai 2019
Commenté : Aep le 6 Mai 2019
Hello everybody,
I have a for loop and I want to use parfor for increasing the speed. The problem is that I cannot modify my parfor code so that the Matlab can run it. Considering we have a vector called "point" and a matrix called "A" so that:
point=[3 4 5 6]
A=sparse(10)
I want to modify some of the arrays of matrix "A" based on the values form vector "point":
parfor j=1:size(point,2)
A(point(j),point(j)+2)=10;
end
The Matlab gives this error: "The variable A in a parfor cannot be classified."
Can anybody help me with this problem?
I have actually seen the Matlab documentation and I have tried to modify my code based on those instructions, but it still doesn't work. Actually, my real "point" vector and "A" matrix are much larger than what I wrote here, but the main problem with my code is what I mentioned here.
Thanking you in anticipation

Réponse acceptée

Walter Roberson
Walter Roberson le 6 Mai 2019
This cannot be done. The locations modified by a given parfor iteration must be easily calculated by the parfor index. When you look up the index in point() then parfor cannot easily prove that no two iterations will ever want to write to the same place.
Updating a sparse array in parfor is a bit questionable to me due to the way that data is stored in sparse arrays. Perhaps (hypothetically) it might return a partial result that gets updated in the client for consistency, but I do not know. That is a detail that should be checked.
You should perhaps instead create vectors of locations to update and corresponding values and then after the loop use sparse() to do the updates.
  3 commentaires
Walter Roberson
Walter Roberson le 6 Mai 2019
L1(j) = point(j) ;
L2(j) = point(j) + 2;
V(j) = 10;
Outside the loop
sparse(L1, L2, V)
Aep
Aep le 6 Mai 2019
Thank you very much. Your answer helped me a lot.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing 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!

Translated by