while loop dose not update the variable :(

hello, i am trying to update the value of n in the while loop, but n seem to be equal to 1 all the time, which means that the while loop didn't work.. can you please help the graph should be exponentially decreasing.
b=0;
bits=32;%length of bits in the packet
E=zeros(1,11);
N=zeros(1,11);
for e=0:0.05:0.5
packet=round(rand(1,bits));% to make sure the random numbers in the packet contain 1's or 0's in an array
ndata =bsc(packet,e);
totbits=bits;
cbits=bits;
n=cbits/totbits; %correct bits over total bits
while packet~=ndata
ndata =bsc(packet,e);
j=abs(packet-ndata);
s=sum(j,2);
c=bits-s;
cbits=cbits+c;
totbits=totbits+bits;
n=cbits/totbits %correct bits over total bits
end
b=b+1
E(b)= e
N(b)= n
end
plot(E,N)
grid on

댓글 수: 2

Geoff Hayes
Geoff Hayes 2017년 4월 22일
편집: Geoff Hayes 2017년 4월 22일
Noora - what is the bsc function? Is it Model binary symmetric channel? Have you tried stepping through the code (using the MATLAB debugger) to see why the condition on the while loop is never true? Presumably this is the case since you state that your variable (which one?) doesn't update...
Sudarshan Kolar
Sudarshan Kolar 2017년 4월 24일
편집: Sudarshan Kolar 2017년 4월 24일
Hello Noora,
I understand that you do not see the values updating in the while loop. I would recommend stepping through your code to better understand what is happening. The following documentations will walk you through code debugging and stepping in Matlab:
https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html
Please follow the doc below to observe the values of your variable as you step:
https://www.mathworks.com/help/matlab/matlab_prog/examine-values.html
Hope that helps.
Sudarshan

댓글을 달려면 로그인하십시오.

답변 (1개)

James Tursa
James Tursa 2017년 4월 24일
편집: James Tursa 2017년 4월 24일

0 개 추천

To test if vectors are not equal to each other in an if-test, using the element-wise operator ~= will produce a vector result and then the if-test condition will be a vector. Is this what you intended? Maybe use a different method to compare for inequality here. E.g.,
% while packet~=ndata
while ~isequal(packet,ndata)
Although when I make this change, it appears to generate an infinite loop ...

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2017년 4월 22일

편집:

2017년 4월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by