Problem in using parfor for matrix

2 Ansichten (letzte 30 Tage)
Aep
Aep am 6 Mai 2019
Kommentiert: Aep am 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

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 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 Kommentare
Walter Roberson
Walter Roberson am 6 Mai 2019
L1(j) = point(j) ;
L2(j) = point(j) + 2;
V(j) = 10;
Outside the loop
sparse(L1, L2, V)
Aep
Aep am 6 Mai 2019
Thank you very much. Your answer helped me a lot.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by