MATLAB: Is the pile-up of ones into the elements of a matrix parallelizable by parfor

accumarrayParallel Computing Toolboxparallelizing accumarray

Hello,
Is there any way to do the following pile-up in matrix A using parfor?
A = zeros(1,J);
for i=1:n
j = f(i); % f returns index j = 1,2,...J
A(j) = A(j)+1;
end
Thanks in advance, Abi

Best Answer

You don't really want the A(j)=A(j)+1 inside the PARFOR, because that's not a data parallel operation. Presumably f(i) is the hard computation, in which case you could do,
A=zeros(J,1);
j=ones(n,2);
parfor i=1:n
j(i,1) = f(i); % f returns index j = 1,2,...J
end
A=accumarray(j,1,size(A)).'