matlab - How does sprintf('%d',A) - '0' work? -
i looking way separate digits of array in matlab i.e. if a = 1024
a = [1, 0, 2, 4]
.
i searched on net , found code (also posted on title):
sprintf('%d',a) - '0'
which converted [1024]
-> [1, 0, 2, 4]
.
it did solve problem did not understand it, - '0'
part. can please explain how works?
also if write sprintf('%d',a) + '0'
(for a = [1024]
) in matlab command window showed following:
97 96 98 100
this puzzled me more can explain this?
it takes advantage of automatic casting char array double array when -
operator used. remember each character has ascii value if type double('0')
in command line , you'll see 48
answer. while double('1024')
gives you
ans = 49 48 50 52
sprintf('%d', a)
convert integer string (i.e. char array). minus casts both sides double end with
double('1024') - double('0')
which is
[49, 48, 50, 52] - [48]
which ends [1,0,2,4]
from here should clear why adding '0'
resulted in [97, 96, 98, 100]
Comments
Post a Comment