Summation of a character

조회 수: 8 (최근 30일)
Edward Huang
Edward Huang 2019년 6월 20일
편집: Stephen23 2019년 6월 20일
I was working on Cody Problem 74 where I found examples like:
sum('13') = 100
sum('22') = 100
sum('123456') = 309
I wonder how it works here, i.e., sums a character but returns a double.
Thanks in advance.

채택된 답변

Stephen23
Stephen23 2019년 6월 20일
편집: Stephen23 2019년 6월 20일
Take a look at the character codes, e.g.:
>> double('13')
ans =
49 51
The function sum simply sums up the character codes:

추가 답변 (1개)

KSSV
KSSV 2019년 6월 20일
Read about uint16. This will convert the characters into numeric codes. When you use sum on the characters, they will be converted to uint16 numbers and gets summed up. Check below code.
a = uint16('13') ;
b = uint16('22') ;
c = uint16('123456') ;
In the above sum(a) and sum('13') are same.

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by