Convert a for loop to parfor loop
4 views (last 30 days)
Show older comments
Hi,
In order to make my code faster, I am trying to convert for loop to parfor loop.
The original subpart of my code is (subject to error)
SNR_ind = -5:2.5:10;
run = 100;
count = 1;
for SNR_ind = SNR
error_NP = [];
error_SPVP = [];
parfor i = 1:run
%% Measurement
[S_IC,S_IT,W_T,W_C] = noise_gen(SNR_ind,SIR,A_I,S_IC1,S_IT1,I,N_I,Avg_eeg_S.avg,t_index);
[Avg_eeg_IC,~] = source_modeling(vol,elec,d_I_loc,t_index,S_IC); % With new S_IC,S_IT
[Avg_eeg_IT,dip_posm] = source_modeling(vol,elec,d_I_loc,t_index,S_IT);
X_T = Avg_eeg_S.avg + Avg_eeg_IT.avg + W_T;
X_C = Avg_eeg_IC.avg + W_C;
%% Inverse localization
[error] = Null_proj(leadfield_eeg_scan,N_S,N_I,X_C,X_T,zplane,mri_resliced,d_S_loc);
error_NP = [error_NP error];
[error] = SPVP_proj(leadfield_eeg_scan,N_S,N_I,X_C,X_T,zplane,mri_resliced,d_S_loc);
error_SPVP = [error_SPVP error];
end
Eerror_NP(count,:) = error_NP;
Eerror_SPVP(count,:) = error_SPVP;
count = count+1;
end
I have used parfor in inner loop as the inner loop will run more times than the outer loop.
I am getting error - " Error: The temporary variable 'S_IC' must be set before it is used. For more information, see Parallel for Loops in MATLAB, "Uninitialized Temporaries"."
I do understand the problem that variable S_IC need to be used in the very next statement it is calculated. But I don't know how to work around. I've read the documentation, but i can't figured out how to convert into a parfor loop. How should I use parfor in this context?
2 Comments
Walter Roberson
on 9 Feb 2020
error_NP = [error_NP error];
Do not do that. Assign to a row or column instead. Or if there is a different output size then assign into a cell array and convert to numeric vector afterwards.
Accepted Answer
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!