Why am I getting 'NaN' values in my vector?
조회 수: 7 (최근 30일)
이전 댓글 표시
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 :-)
댓글 수: 0
답변 (2개)
Matt J
2020년 11월 15일
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);
댓글 수: 0
Ameer Hamza
2020년 11월 15일
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!