Save variable to a matrix after a for loop

5 views (last 30 days)
Hey guys this is probably very easy, but Im having troubles doing it.
I have this code, that cross-correlates two signals, from 2 matrixes. I would like to save all the outputs of the function in 3 matrixes(afmag3,delay3,doppler3). Also the code, takes too long to run , I dont know if this can be better. Thank you
for i=1:m
for ii=1:m2
[afmag3,delay3,doppler3] = ambgfun(Reference_SignalCut,Surveillance_SignalCut,fs,[1e6 1e6]);
afmag3 = afmag3*1; % Select plot gain *1
afmag3(afmag3>1 )= 1;

Accepted Answer

Jan on 11 Jun 2022
Edited: Jan on 11 Jun 2022
[n, m] = size(surv_matrix);
[n2, m2] = size(ref_matrix);
afmag3 = cell(m, m2);
delay3 = cell(m, m2);
doppler3 = cell(m, m2);
for i=1:m
S = surv_matrix(:,i); % Surveillance of cut signal
for ii=1:m2
R = ref_matrix(:,ii); % Reference of cut signal
[tmp, delay3{i, ii}, doppler3{i, ii}] = ambgfun(R, S, fs, [1e6 1e6]);
afmag3{i, ii} = max(1, tmp);
Concerning the run time, use the profile 'r to finde the bottleneck. If ambgfun could be vectoprized maybe a speedup is possible.
I've moved the definition of Surveillance_SignalCut out of the inner loop. This does not save a lot of time, but it is a good programming practice not to do any work repeatedly. afmag3 = afmag3*1 was a waste of time only.
Avoid complicated names for variables, because this impedes the reading. Explain the meaning of a variable in a comment instead.

Sign in to comment.

More Answers (0)


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!

Translated by