Problem in using parfor for matrix
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2019-5-6
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 个评论
Walter Roberson
2019-5-6
L1(j) = point(j) ;
L2(j) = point(j) + 2;
V(j) = 10;
Outside the loop
sparse(L1, L2, V)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!