colon operator bug?
2 views (last 30 days)
Show older comments
Can Atalay on 2 Apr 2022
Answered: Can Atalay on 3 Apr 2022
Is creating an array by saying;
x = 1:5;
not robust? Because
returned something like
And I got an
Array indices must be positive integers or logical values.
error when trying to index into another array using my "x".
I can share the real script (which is pretty long) and the files (1+ GB) but wanted to ask the simpler version first. If the colon operator for sure works perfect I should probably go spend some more time before posting
Riccardo Scorretti on 2 Apr 2022
Edited: Riccardo Scorretti on 2 Apr 2022
There is no reason to observe the described behaviour. By the way, I cannot reproduce:
x = 1:5;
format long ; x(3)
Are you sure there isn't something else? -- I mean, some code between the definition of x and x(3)?
More Answers (1)
Jan on 2 Apr 2022
The colon operator works as expected. For integer values it does reply integers. But be aware of rounding effects for floating point values:
a = 0:0.1:1
any(a == 0.3) % FALSE!
Stephen23 on 2 Apr 2022
Edited: Stephen23 on 2 Apr 2022
"because 0.1 is periodic in base 1"
Can you please show us 0.1 written in base 1. Or any fractional number in base 1, for that matter.
"But here, it should not be the case."
Which is more likely:
- COLON has a major bug which thousands and thousands of users never noticed.
- The OP's code does not do what they think it is doing.
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!