How to store all FOR loop iteration in a vector and plot every iteration?

I want to save the data of every iteration in a vector, and plot it later . Could anyone give me a hint on how to do this ?
for a=1:114
power=pote(a,:);
mean_power_50=filter(ones(1,50)/50,1,power);
power_fading=power-mean_power_50;
x1=power_fading(1:1000);
x2=power_fading(1001:2001);
x3=power_fading(2002:3002);
x4=power_fading(3003:4003);
x5=power_fading(4004:5000);
end

 Réponse acceptée

The loop is not needed at all, filter can operate on the column of whole matrices at once. However, since you're working on rows, you'll have to transpose your matrix (and transpose the result):
mean_power_50 = filter(ones(1,50)/50,1, pote.').'; %work on the whole array
power_fading = pote - mean_power_50;
You can then split it into subarrays. However do not use numbered variables. Instead put these subarrays altogether into a cell array or other container:
%assuming that power_fading is 5000 columns:
x = mat2cell(power_fading, size(power_fading, 1), [1000 1001 1001 1001 997]); %any reason why it's not 1000 for each submatrix?

9 commentaires

yusra Ch
yusra Ch le 12 Oct 2016
Modifié(e) : yusra Ch le 12 Oct 2016
Hi Guillaume , thank you, well my data represents the mesearment of the path loss in vehicule to vehicule communication, the reason behind diffrent size is that I have to eleminate the 50 first shadowing components that's why.
yusra Ch
yusra Ch le 12 Oct 2016
Modifié(e) : yusra Ch le 12 Oct 2016
Hi again,
I want to work with numbered variables becquse I need to plot every one of them after the for loop . can you plz tell me how to get the result for every iteration to plot it after the for loop. My code is like this :
>>for a=1:114
power=pote(a,:);
mean_power_50=filter(ones(1,50)/50,1,power);
power_fading=power-mean_power_50;
x1=power_fading(51:1051);
x2=power_fading(1052:2052);
x3=power_fading(2053:3053);
x4=power_fading(3054:4054);
x5=power_fading(4055:5000);
end
%---------------------------------------------Plot the results------------------------------------------------------%
Fig1=figure;
plot(power,'b');
xlabel('Numero de trazas');
ylabel('Power(dB)');
hold on;
plot(mean_power_50,'r');
%--------------traze 1- ventana 1------------------ %
Fig2=figure;
plot(linspace(-1000,1000,2001)*1.03,xcorr(x1,'coeff')) %
hold on
plot(linspace(-1000,1000,2001)*1.03,xcorr(x1,'coeff'),'or')
title('ventana 1 [1:1000]')
hold on
% % % %--------------------------- traze 1- ventana 2---------
fig3=figure;
plot(linspace(-1000,1000,2001)*1.03,xcorr(x2,'coeff'))
hold on
plot(linspace(-1000,1000,2001)*1.03,xcorr(x2,'coeff'),'or')
title('ventana 2 [1001:2001]')
.....
same thing for X3,X4 and x5
I want to make a declare a variable as a vector and initiate the counter . the idea is to get the results of x1 , x2, x3, x4 and x5 for every iteration and plot them , but this code gives me just the plots of the last iteration .
power=[];
count=1;
for a=1:114
power=pote(a,:);
mean_power_50=filter(ones(1,50)/50,1,power); % the real one
power_fading=power-mean_power_50;
x1=power_fading(51:1051);
x2=power_fading(1052:2052);
x3=power_fading(2053:3053);
x4=power_fading(3054:4054);
x5=power_fading(4055:5000);
count=count+1;
end
Well, here you go, you've just shown an example why numbering variables is an extremely bad idea. You end up having to write the same code several times with just the variable name (and a few other things depending on that name) changing each time.
Instead, if you all put them into a cell array, a simple loop does it all:
mean_power_50 = filter(ones(1,50)/50,1, pote.').'; %work on the whole array
power_fading = pote - mean_power_50;
ranges = [51 1051;
1052 2052;
2053 3053;
3054 4054;
4055 5000]; %each row is the start and end of index range
hfig = gobjects(size(ranges, 1), 1); %preinitialise array of figure handles
power_split = cell(size(range, 1, 1); %container for portions of power fading according to range
for idx = 1:size(ranges, 1)
power_split{idx} = power_fading(:, range(idx, 1):range(idx, 2));
hfig(idx) = figure;
hold on;
toplot = xcorr(power_split{idx}, 'coeff');
plot(linspace(-1000, 1000, size(toplot, 1)) * 1.03, toplot);
plot(linspace(-1000, 1000, size(toplot, 1)) *1.03, toplot, 'or');
title(sprintf('Ventana %d [%d:%d]', idx, ranges(idx, 1), ranges(idx, 2));
end
yusra Ch
yusra Ch le 12 Oct 2016
Modifié(e) : yusra Ch le 12 Oct 2016
I have tried your code and it's taking a lot of time to compilate I don't know why
Guillaume can you plz explain tome how this code works ??
I don't have/use matlab coder, so I have no idea what the requirements are for a successful compilation. However, there is nothing complex in my code, so I don't see why compilation would be long.
What is it you don't understand in the code? As said, it applies the filter on the whole matrix at once. The rest of the code is just looping over the various ranges you want to plot and plots them.
Thank you so much Guillaume. The simulation still running but this errors are appearing :
Error using ifft Out of memory. Type HELP MEMORY for your options.
Error in xcorr>autocorr (line 198) c1 = real(ifft(C,[],1));
Error in xcorr (line 126) c1 = autocorr(x,maxlag);
Error in LOS_matrice (line 18) toplot = xcorr(power_split{idx}, 'coeff');
The error tells you that it's running out memory when computing the inverse fourier transform during your cross correlation. This would be caused by the matrix passed to xcorr having too many columns, which may be due to a mistake I made.
If your signals are arranged by rows, then the matrix needs to be transposed before passing it to xcorr, so if you change the toplot line to:
toplot = xcorr(power_split{idx}.', 'coeff');
does it work better?
Unfortunately, I don't have the signal processing toolbox nor matlab coder so I can't really test the answer I gave you.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by