Finding self-locating loops in pi, receiving "Index exceeds the number of array elements. Index must not exceed 50."

조회 수: 1 (최근 30일)
Hi, as per title I came into this error. Is this related to memory or perferences perhaps?
% Define the number of digits to use from pi
numDigits = 100000;
% Load the first numDigits digits of pi into a string
piStr = num2str(pi, numDigits);
% Loop over all possible loop lengths
for loopLen = 1:floor(numDigits/2)
% Loop over all possible starting positions for the loop
for startPos = 1:numDigits-loopLen*2
% Extract the loop and the following sequence from piStr
loopStr = piStr(startPos:startPos+loopLen-1);
seqStr = piStr(startPos+loopLen:startPos+loopLen*2-1);
% Check if the loop appears again in seqStr
if contains(seqStr, loopStr)
fprintf('Found self-locating loop of length %d\n', loopLen);
fprintf('Loop starts at position %d\n', startPos);
fprintf('Loop sequence: %s\n', loopStr);
fprintf('Following sequence: %s\n', seqStr);
end
end
end
Found self-locating loop of length 1
Loop starts at position 18
Loop sequence: 1
Following sequence: 1
Found self-locating loop of length 1
Loop starts at position 21
Loop sequence: 9
Following sequence: 9
Found self-locating loop of length 1
Loop starts at position 31
Loop sequence: 4
Following sequence: 4
Index exceeds the number of array elements. Index must not exceed 50.

답변 (1개)

chicken vector
chicken vector 2023년 6월 1일
편집: chicken vector 2023년 6월 1일
The error comes from the fact that 50 is the maximum number of digits for pi.
You can have a proof of this by doing:
nDigits = 60;
num2str(pi, nDigits)
ans = '3.141592653589793115997963468544185161590576171875'
num2str(pi, ['%.' num2str(nDigits) 'f'])
ans = '3.141592653589793115997963468544185161590576171875000000000000'
Note that double precision floating point numbers have 16 digit of accuracy.
  댓글 수: 1
Stephen23
Stephen23 2023년 6월 1일
편집: Stephen23 2023년 6월 1일
50 is anyway well beyond the limits of double binary floating point:
3.141592653589793115997963468544185161590576171875 % 50 DOUBLE
3.141592653589793238462643383279502884197169399375105820974944 ... actual
% ^ oops

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

카테고리

Help CenterFile Exchange에서 Timing and presenting 2D and 3D stimuli에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by