how do i store a matrix value inside a parfor loop?

I wanted to use the A matrix outside the parfor loop for further computation, i get an error stating "cannot run due to way variable A", is there any solution to this problem ??is there any other way to save the matrix???
thank you
N=50;
parfor n=1:Nc
b=rand(300,103);
[Q,R]=qr(b,0);
A((n-1)*(N+1)+1:n*(N+1),:)=R
end

 Respuesta aceptada

Greg
Greg el 17 de Feb. de 2018
Pre-allocate A to store each R along the third dimension, then reshape it outside the loop. Iterations of parfor can't use indexing that depends on other iterations of the loop (i.e., n-1).
A = zeros(N+1,numColsofR,Nc);
parfor ...
A(:,:,n) = R;
end
reshape(permute(A,[1,3,2]),[],numColsofR);

4 comentarios

thank you so much for the answer !!
Greg
Greg el 17 de Feb. de 2018
Happy to help.
This really worked out
What if zeros will consume large memory?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 17 de Feb. de 2018

Comentada:

el 18 de Nov. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by