Fractions as array indices in for loop
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
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
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
2020-2-11
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...
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!