What is the difference between 5 and '5'

조회 수: 4 (최근 30일)
Jonathan Mendoza
Jonathan Mendoza 2019년 8월 27일
댓글: Bruno Luong 2019년 8월 30일
5 + 1 = 6
'5' + 1 = 54
Why does the seceond line equal 54???
What does '' around numbers???
  댓글 수: 2
Star Strider
Star Strider 2019년 8월 27일
To make things even more interesting:
"5" + 1 = "51"
Bruno Luong
Bruno Luong 2019년 8월 30일
Associativity warning
>> "5"+1+2
ans =
"512"
>> 1+2+"5"
ans =
"35"

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

채택된 답변

Bruno Luong
Bruno Luong 2019년 8월 27일
편집: Bruno Luong 2019년 8월 27일
The '5' is the char array. Char array contain a text including number, such as 'Schrodinger has 2 dogs and 1/2 cat'. Char array uses ASCII code internally (actually UNICODE) and the ascii code of '5' is 53, therefore '5'+1 = 53+1 = 54
5 (without quote) is number 5, where the arithmetics operations has standard meaning.
  댓글 수: 2
Jonathan Mendoza
Jonathan Mendoza 2019년 8월 27일
*claps*
Walter Roberson
Walter Roberson 2019년 8월 28일
There are deep historical reasons why the digits are not the first 10 positions in the character codes. It goes back to Morse code and the fact that Morse originally had no digits, and it goes back to mechanical selector lines...
For a while it looked like BCD or later EBCDIC would become the standard. EBCDIC coded '0' as the character after '9'.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by