MATLAB Indexing type dependence (bug?)

조회 수: 2 (최근 30일)
Ben
Ben 2025년 2월 20일
이동: Stephen23 2025년 2월 21일
Please see this strange example, where the datatype (single vs double) of the starting index affects how many data points are returned:
y = rand(67347456+50,1);
y = single(y);
N = single(50);
size(y(N+1:end))
MATLAB returns: >> 67347454 1 (incorrect, missing 2 elements)
size([N+1:length(y)])
MATLAB returns: >> 1 67347456 (which is correct)
and
size(y(double(N)+1:end))
MATLAB returns >> 67347456 1 (also correct)
Other datatypes like uint32 also work fine.
Please help me understand: If N is only 50, what does it matter to the indexing whether it is a single or a double? Also, aren't these ending index values well within the presicion of single?

채택된 답변

Stephen23
Stephen23 2025년 2월 20일
이동: Stephen23 2025년 2월 21일
"what does it matter to the indexing whether it is a single or a double?"
Because single precision cannot represent all of those indices. The largest index is:
y = rand(67347456+50,1);
x = numel(y)
x = 67347506
which cannot be represented using SINGLE. The closest SINGLE value is:
single(x)
ans = single 67347504
"...aren't these ending index values well within the presicion of single?"
Nope, those indices are well above FLINTMAX:
flintmax('single')
ans = single 16777216
"MATLAB Indexing type dependence (bug?)"
I do not see anything that indicates a bug.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2025년 2월 20일
eps(single(67347456+50))
ans = single 8
single precision numbers are 8 apart by the time of 67347456

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by