Save variable in a for loop

6 visualizzazioni (ultimi 30 giorni)
Miguel Albuquerque
Miguel Albuquerque il 11 Lug 2022
Risposto: Harshit il 12 Set 2023
Hey guys, I have this code that correlates two signals with a large amount of samples, each one has 30 million. I want to save the max value of the correlation in a matrix or array, so that in the end of the cycle I can plot all the values and see the evolution of the max correlation.
I want to correlate all the surveillance_signal(30 million) with a small amount of samples of reference_signal(1000). Basically I will correlate these two signal thousand by thousand, spaced by thousand samples, till I reach the end of reference_signal.This means, first iteration:surveillance_signal(30 million) and reference_signal(1:1000), save the max correlation in a array. Second iteration surveillance_signal(30 million) and reference_signal(2000:3000), save the max correlation value.
I have this code, that does what I want, I think, but how can I save the maximum value(xMax3,yMax3) in each iteration so that in the end I can see the evolution in a plot.
reference_reshaped = reshape(Reference_signal, 1000, 4, []);
sz = size(reference_reshaped);
Surveillance_signal_samples=Surveillance_signal;
for i = 1:sz(3)
r = reference_reshaped(:,1,i);
[afmag3,doppler3] = ambgfun(r,Surveillance_signal_samples,Fs,[250000 250000],'Cut','Delay');
afmag3(afmag3>1 )= 1;
[pks3,index3] = max(afmag3);
xMax3 = doppler3(index3);
yMax3 = pks3;
textString3 = sprintf('(%.2e, %f)', xMax3, yMax3);
end
  1 Commento
Miguel Albuquerque
Miguel Albuquerque il 12 Lug 2022
Hey guys, I have done this, correlating 100000 samples with 1000 samples, is this correct?
counter=1;
surveillance_reshaped = reshape(Surveillance_signal, 100000, 2, []);
reference_reshaped = reshape(Reference_signal, 1000, 2, []);
sz = size(surveillance_reshaped);
for i = 1:sz(3)
r = reference_reshaped(:,1,i);
s = surveillance_reshaped(:,1,i);
[afmag3,doppler3] = ambgfun(r,s,Fs,[250000 250000],'Cut','Delay');
afmag3(afmag3>1 )= 1;
[pks3,index3] = max(afmag3);
xMax3 = doppler3(index3);
yMax3 = pks3;
yMax3(:,i)=yMax3;
xMax3(:,i)=xMax3;
counter=counter+1
end

Accedi per commentare.

Risposte (1)

Harshit
Harshit il 12 Set 2023
Hello,
I understand that you want to save the maximum values in each iteration using a for loop so that you can plot the evolution of the max correlation values.
I suggest intializing an array or matrix before the loop to store the values. Inside the loop, the maximum correlation value "yMax3" is stored in the corresponding position of the array. This way, at the end of the loop, the array or matrix will contain all the maximum correlation values from each iteration.
You can then plot the evolution of the max correlation values using the "plot" function or any other plotting method of your choice.
Here's an example of how you can modify your code to achieve this:
reference_reshaped = reshape(Reference_signal, 1000, 4, []);
sz = size(reference_reshaped);
Surveillance_signal_samples = Surveillance_signal;
max_correlation_values = zeros(sz(3), 1); % Initialize array to store max correlation values
for i = 1:sz(3)
r = reference_reshaped(:, 1, i);
[afmag3, doppler3] = ambgfun(r, Surveillance_signal_samples, Fs, [250000 250000], 'Cut', 'Delay');
afmag3(afmag3 > 1) = 1;
[pks3, index3] = max(afmag3);
xMax3 = doppler3(index3);
yMax3 = pks3;
max_correlation_values(i) = yMax3; % Store max correlation value in array
textString3 = sprintf('(%.2e, %f)', xMax3, yMax3);
end
I hope this helps!
Regards,
Harshit

Community Treasure Hunt

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

Start Hunting!

Translated by