Optimize loop to reduce run time
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
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));
endMy 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
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 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
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 for loop, optimization MATLAB Answers — New Questions