colon operator bug?

조회 수: 1 (최근 30일)
Can Atalay
Can Atalay 2022년 4월 2일
답변: Can Atalay 2022년 4월 3일
Is creating an array by saying;
x = 1:5;
not robust? Because
x(3)
returned something like
3.0001221
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
  댓글 수: 1
Riccardo Scorretti
Riccardo Scorretti 2022년 4월 2일
편집: Riccardo Scorretti 2022년 4월 2일
There is no reason to observe the described behaviour. By the way, I cannot reproduce:
x = 1:5;
format long ; x(3)
ans =
3
Are you sure there isn't something else? -- I mean, some code between the definition of x and x(3)?

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

채택된 답변

Can Atalay
Can Atalay 2022년 4월 3일
My bad, turns out I changed a for loop index inside that for loop and appended the index it to an array, and the index turned out not functioning like an integer somehow.
Fixed itself when I corrected the index names - we can delete the question if you guys want

추가 답변 (1개)

Jan
Jan 2022년 4월 2일
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!
  댓글 수: 2
Riccardo Scorretti
Riccardo Scorretti 2022년 4월 2일
Exact, because 0.1 is periodic in base 1. But here, it should not be the case.
Stephen23
Stephen23 2022년 4월 2일
편집: Stephen23 2022년 4월 2일
"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.

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

카테고리

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

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by