Why am I getting 'NaN' values in my vector?

10 views (last 30 days)
Mich Elly
Mich Elly on 15 Nov 2020
Answered: Ameer Hamza on 15 Nov 2020
Hello!
I have been working on this bit of code in Matlab which deals with two vectors, 'x' and 'y'. The code I am working on in Matlab is based upon a set of instructions, shown in the image below.
%initialising the first 18 elements of the x sequence
x(1)=1;
for i=2:18
x(i)=0;
end
%initialising the first 18 elements of the x sequence
for i=1:18
y(i)=1;
end
%performing recursive assignments
for i = 0:262124
x(i+18+1) = x(i+7+1) + mod(x(i+1),2);
y(i+18+1) = y(i+10+1) + y(i+7+1) + y(i+5+1) + mod(y(i+1), 2);
end
I have noticed that in my 'y' vector, I get 'NaN' and 'Inf' values from the 6755 index onwards. I have been checking my Matlab code against the "reference" code (in the image) that I am trying to implement, and I can't seem to pick up where I am going wrong as I should be getting integers. For context, this code is a part of a tellecommunications scrambling code.
I appreciate any ideas. Thank you :-)

Answers (2)

Matt J
Matt J on 15 Nov 2020
I don't understand the instructions, but maybe this is what it means:
x(i+18+1) = mod( x(i+7+1) + x(i+1), 2);
y(i+18+1) = mod( y(i+10+1) + y(i+7+1) + y(i+5+1) + y(i+1), 2);

Ameer Hamza
Ameer Hamza on 15 Nov 2020
This happens because MATLAB uses double() datatype, which uses the IEEE-754 double-precision format: https://en.wikipedia.org/wiki/Double-precision_floating-point_format. The maximum number which can be expressed in this format is of the order ~$10^{308}$. If you check the value in the 6755-th element of the array, you can see it is around that value. There is no easy workaround for this other than to use variable precision mathematics from the symbolic toolbox but bear in mind that it can be very slow as compared to the double-precision calculations.

Tags

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!