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
채택된 답변
Guillaume
2016년 10월 11일
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
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.
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
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.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Correlation and Convolution에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
