MATLAB: Can’t get the real result for addition and division using basic arithmetic operations. pls help..

addictionarithmeticbasic operationsdivideMapping ToolboxoperationsplussubstractionSymbolic Math Toolbox

I have 3 values
jum1 = num2str(length(B1))
jum2 = num2str(length(B2))
jum4 = num2str(sum(roundObjects))
run on command window (the real values) :
jum1 =
12
jum2 =
62
jum4 =
2
but when I adding for example
rasio = jum2+jum4
in command window
rasio =
104 100
and when I use another function like this
rasio = (jum1 / (jum2 + jum4))
in command window
rasio =
0.4850
I think all results of my function not show the real values as I wish. Is there anything wrong of my functions??

Best Answer

  • num2str() does not create symbolic variables: it converts the numbers into strings of characters. The internal representation of the character '1' is not 1; the internal representation for the character '1' happens to be 49. When you do the addition, you are not adding 62 + 2, you are adding ['6' '2'] + '2' which is internally [54 50] + 50, which gives you the [104 100] that you see.
    The characters '0' through '9' are internally 48 through 57. char(48) == '0'.
    The reasons that the characters '0' through '9' are not represented internally as the decimal values 0 through 9 are historical, involving mechanical movements on "automatic telegraphs" and 5 bit codes with "shift select" characters as needed to move between ranges. Decisions taken like 100 years ago that got merged into standards that still carry through (because there is no good reason to change them.)