Problem in using parfor for matrix
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Walter Roberson
on 6 May 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 Comments
Walter Roberson
on 6 May 2019
L1(j) = point(j) ;
L2(j) = point(j) + 2;
V(j) = 10;
Outside the loop
sparse(L1, L2, V)
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!