Fractions as array indices in for loop

Why isn't it possible to use fractions as array indices?
I know it won't work, but why doesn't it work and are there ways to work around this?

댓글 수: 3

Stephen23
Stephen23 2020년 2월 11일
편집: Stephen23 2020년 2월 12일
All indexing is ultimately just a mapping of the ordinal numbers onto positions of values/elements of an array.
By definition, any sequence of numbers, fractional or whole, that are placed into some order, are counted by the ordinal numbers. Even if you defined an indexing system with fractional values, they would still map to the ordinals. So using ordinals is the simplest indexing by far.
You can easily achieve that mapping yourself, using interpolation, ismember, or similar.
The more likely explanation of your question is that you are confusing data with code, in which case you need to simply interpolate data, and not try to invent fractional indices.
dpb
dpb 2020년 2월 11일
That's good insight to bring in the data aspect as perhaps the unstated but underlying question, Stephen... +1.3
Walter Roberson
Walter Roberson 2020년 2월 12일
Under your proposal, given an array with dimension 100, what location would 0.13 correspond to? 0.135? 1/7? 0? sqrt(2)? Would the fraction 3/6 be different than 6/12?
Would your answer about 0.13 change if you knew that in MATLAB, 0.13 has to be approximated by 0.13000000000000000444089209850062616169452667236328125 and so that if you just multiply the fraction by the length of the dimension then except for some special fractions, you do not generally end up with a nice integer?

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

답변 (1개)

dpb
dpb 2020년 2월 11일

0 개 추천

Short answer? "Because!" TMW hath so decreed.
I'm sure it's mostly performance related as well as ease of code development and maintenance. Having to wrap every array access with a rounding operation would be a real performance bottleneck.
Undoubtedly other reasons as well...

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2019b

질문:

2020년 2월 11일

댓글:

2020년 2월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by