Optimize loop to reduce run time
1 次查看(过去 30 天)
显示 更早的评论
My code works well but it takes a lot of time to run since the vectors are larger in size. I know it can be optimized by using vector instead of for loop but I am unable to do that properly. Please help
%% Real Data Based Simulation - Received Signal + Noise only
num_trials = 4000;
noise_sigma = linspace(-60,40,125);
threshold = sqrt(noise_sigma.^2 / 2) * qfuncinv(prob_false_alarm);
prob_det_glrt_signoise_fix_pfa_2d = zeros(num_trials,length(noise_sigma));
prob_det_glrt_signoise_fix_pfaa_2d = zeros(1,length(noise_sigma));
inv_sqrt = 1/sqrt(2);
for jj = 1:length(noise_sigma)
for ii = 1:num_trials
complex_awgn = noise_sigma(jj)*(randn(numrangebins,numdoppbins)+1i*randn(numrangebins,numdoppbins))*inv_sqrt;
s_matrix_2D = u_matrix_2D + complex_awgn; % received signal with noise
pow_val_2D = (abs (s_matrix_2D(bin_rng_2d,bin_dopp_2d)) )^2;
prob_det_glrt_signoise_fix_pfa_2d(ii,jj) = pow_val_2D > threshold(jj); % Hypothesis H1
end
prob_det_glrt_signoise_fix_pfaa_2d(jj) = mean(prob_det_glrt_signoise_fix_pfa_2d(:,jj));
end
1 个评论
Torsten
2024-5-21
To run the code without a for-loop, you would need 3d-random matrices of size numrangebins x numdoppbins x numtrials.
Since my guess is that "numtrials" is large, I don't think it is a good idea concerning memory to vectorize in your case.
回答(2 个)
Tony
2024-5-21
Since the calculations of the inner-most loop are independent among its iterations, you can parallelize the computations (run multiple calculations in parallel at the same time), provided you have the Parallel Computing Toolbox. Below I've made simple modifications to your code to show how it works (main difference is for to parfor). But as you can see, currently the parallelization is doing worse because of the overhead of copying the variables needed for the computation for each core. So this would require some work to make it tractable for your situation.
% setting dummy values for testing
prob_false_alarm = 0.5;
qfuncinv = @(x) x;
numrangebins = 1;
numdoppbins = 1;
bin_rng_2d = randi(numrangebins);
bin_dopp_2d = randi(numdoppbins);
u_matrix_2D = randn(numrangebins,numdoppbins);
%% Real Data Based Simulation - Received Signal + Noise only
num_trials = 4000;
noise_sigma = linspace(-60,40,125);
threshold = sqrt(noise_sigma.^2 / 2) * qfuncinv(prob_false_alarm);
prob_det_glrt_signoise_fix_pfa_2d = zeros(num_trials,length(noise_sigma));
prob_det_glrt_signoise_fix_pfaa_2d = zeros(1,length(noise_sigma));
inv_sqrt = 1/sqrt(2);
%% Without Parallelization
tic;
for jj = 1:length(noise_sigma)
prob_det_glrt_signoise_fix_pfa_2d_jj = true(num_trials,1);
for ii = 1:num_trials
complex_awgn = noise_sigma(jj)*(randn(numrangebins,numdoppbins)+ii*randn(numrangebins,numdoppbins))*inv_sqrt;
s_matrix_2D = u_matrix_2D + complex_awgn; % received signal with noise
pow_val_2D = (abs (s_matrix_2D(bin_rng_2d,bin_dopp_2d)) )^2;
prob_det_glrt_signoise_fix_pfa_2d_jj(ii) = pow_val_2D > threshold(jj); % Hypothesis H1
end
prob_det_glrt_signoise_fix_pfaa_2d(jj) = mean(prob_det_glrt_signoise_fix_pfa_2d_jj);
end
toc;
%% With Parallelization
tic;
for jj = 1:length(noise_sigma)
prob_det_glrt_signoise_fix_pfa_2d_jj = true(num_trials,1);
parfor ii = 1:num_trials
complex_awgn = noise_sigma(jj)*(randn(numrangebins,numdoppbins)+ii*randn(numrangebins,numdoppbins))*inv_sqrt;
s_matrix_2D = u_matrix_2D + complex_awgn; % received signal with noise
pow_val_2D = (abs (s_matrix_2D(bin_rng_2d,bin_dopp_2d)) )^2;
prob_det_glrt_signoise_fix_pfa_2d_jj(ii) = pow_val_2D > threshold(jj); % Hypothesis H1
end
prob_det_glrt_signoise_fix_pfaa_2d(jj) = mean(prob_det_glrt_signoise_fix_pfa_2d_jj);
end
toc;
0 个评论
Ahmed A. Selman
2024-5-21
编辑:Ahmed A. Selman
2024-5-21
Inside the loops of ii and jj, try to move the lines that do not depend on ii out of that loop. It might help to improve some runtime. Try:
for jj = 1:length(noise_sigma) complex_awgn = noise_sigma(jj)*(randn(numrangebins,numdoppbins)+1i*randn(numrangebins,numdoppbins))*inv_sqrt; s_matrix_2D = u_matrix_2D + complex_awgn; % received signal with noise pow_val_2D = (abs (s_matrix_2D(bin_rng_2d,bin_dopp_2d)) )^2; for ii = 1:num_trials prob_det_glrt_signoise_fix_pfa_2d(ii,jj) = pow_val_2D > threshold(jj); % Hypothesis H1 end prob_det_glrt_signoise_fix_pfaa_2d(jj) = mean(prob_det_glrt_signoise_fix_pfa_2d(:,jj)); end
If it didn't work, please consider giving us the values of this part of your code, there are many variables that need to be defined before we can run it. Regards.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!