Optimize loop to reduce run time

3 Ansichten (letzte 30 Tage)
Sara Nasir
Sara Nasir am 21 Mai 2024
Bearbeitet: Ahmed A. Selman am 21 Mai 2024
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 Kommentar
Torsten
Torsten am 21 Mai 2024
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.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Tony
Tony am 21 Mai 2024
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;
Elapsed time is 0.148096 seconds.
%% 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;
Elapsed time is 1.953571 seconds.

Ahmed A. Selman
Ahmed A. Selman am 21 Mai 2024
Bearbeitet: Ahmed A. Selman am 21 Mai 2024

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.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by