필터 지우기
필터 지우기

Vectorize a nested loop

조회 수: 3 (최근 30일)
federico nutarelli
federico nutarelli 2022년 11월 28일
편집: Matt J 2022년 11월 28일
Hi all,
is there a way to vectorize the following loop?
for RIPETIZIONE = 1:10
%for k = 1:size(lambda_tol_vector.Value,1)
parfor (k = 1:size(lambda_tol_vector.Value,1))
%fprintf('Completion using nuclear norm regularization... \n');
[CompletedMat,flag] = matrix_completion_nuclear_GG_vec( ...
A.Value.*double(B), double(B), N, lambda_tol_vector.Value(k), tol);
if flag==1
CompletedMat_parvec{RIPETIZIONE,k}=zeros(size(A));
end
CompletedMat_parvec{RIPETIZIONE,k}=CompletedMat;
end
end
  댓글 수: 5
federico nutarelli
federico nutarelli 2022년 11월 28일
편집: federico nutarelli 2022년 11월 28일
Oh sorry @Jan.
I will attach the function here.
As for pre-allocation I will check the best way to pre-allocate vell arrays.
federico nutarelli
federico nutarelli 2022년 11월 28일
Oh @Jan another thing that I am nnoticing when putting the arfor loop on the outer one, is tht the final results in term of CompletedMat_parvec is different from the same result when I use only for loos (both outer and inner). Is there a way to make the two results equal?

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

채택된 답변

Matt J
Matt J 2022년 11월 28일
편집: Matt J 2022년 11월 28일
EDITED:
R=10;
K=size(lambda_tol_vector.Value,1);
CompletedMat_parvec=cell(R,K);
B=double(B);
parfor n=1:R*K
[RIPETIZIONE,k]=ind2sub([R,K],n);
%fprintf('Completion using nuclear norm regularization... \n');
[CompletedMat,flag] = matrix_completion_nuclear_GG_vec( ...
A.Value.*B,B, N, lambda_tol_vector.Value(k), tol);
if ~flag
CompletedMat_parvec{n}=CompletedMat;
end
end
idx=cellfun('isempty', CompletedMat_parvec);
CompletedMat_parvec(idx)={zeros(size(A))};
  댓글 수: 4
federico nutarelli
federico nutarelli 2022년 11월 28일
편집: federico nutarelli 2022년 11월 28일
@Matt J same problem unfortunately. Maaybe we should index it by n? Thhough I don't think so since ideally I need CompletedMat_parvec{RIPETIZIONE,k}=...;
Furthermore it looks like the variable n is not creted when evaluaating the parfor loop.
Matt J
Matt J 2022년 11월 28일
편집: Matt J 2022년 11월 28일
Yes! Sorry, you should be indexing by n,
if ~flag
CompletedMat_parvec{n}=CompletedMat;
end
It is equivalent to,
CompletedMat_parvec{RIPETIZIONE,k}=CompletedMat;

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by