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

조회 수: 10 (최근 30일)
Naveen kumar Elumalai
Naveen kumar Elumalai 2018년 2월 17일
댓글: ahmad eldeeb 2022년 11월 18일
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

채택된 답변

Greg
Greg 2018년 2월 17일
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
Naveen kumar Elumalai
Naveen kumar Elumalai 2018년 2월 17일
This really worked out
ahmad eldeeb
ahmad eldeeb 2022년 11월 18일
What if zeros will consume large memory?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by