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 Comments

Geoff Hayes
Geoff Hayes on 22 Apr 2017
Edited: Geoff Hayes on 22 Apr 2017
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...
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

Sign in to comment.

Answers (1)

James Tursa
James Tursa on 24 Apr 2017
Edited: James Tursa on 24 Apr 2017
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 ...

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 22 Apr 2017

Edited:

on 24 Apr 2017

Community Treasure Hunt

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

Start Hunting!