Addition of numbers in array as string
조회 수: 3 (최근 30일)
이전 댓글 표시
I am following these lines of code, but generating error when I do:
x = randi([0,9],1,20);
y = randi([0,9],1,20);
x_new = str2double(sprintf('%d',x));
y_new = str2double(sprintf('%d',y));
result = num2str(x_new+y_new) - '0';
fprintf('%4d',x);
fprintf('\n');
fprintf('%4d',y);
fprintf('\n');
disp('+');
disp('-------------------------------------------------------------------------')
fprintf('%d',result);
fprintf('\n')
The ouput I get (which is wrong):
9 6 1 3 0 5 4 9 8 4 8 1 3 9 9 7 6 3 9 1
7 6 8 3 7 8 3 5 9 5 3 6 3 7 4 4 6 9 3 8
+
-------------------------------------------------------------------------
1-272968385801777453-520
can someone please mention the mistake in running these lines. And help me to align (with same gap spaces) the result
the will be the addition of these numbers.
Thanks
댓글 수: 0
답변 (1개)
Walter Roberson
2021년 2월 15일
double('1')
double('2')
'1' + '2'
char(ans)
MATLAB makes absolutely no attempt to recognize that if you add '1' + '2' that you might be wanting it to mean add 1 and 2.
char(('1' + '2') - '0')
That works, but that isn't what you did.
num2str('1' + '2') - '0'
That's what you did. You got the printable representation of the sum and subtracted '0' from that, whereas what you needed was to subtract '0' from the sum.
But even then...
'3' + '8' - '0'
char(ans)
When you do arithmetic on characters, you have to allow for overflow.
I suggest you subtract '0' from each of the characters, line the two up so that the right side matches and zero-pad on the left if need be. Now add, getting a vector of decimal values. Now run through the vector starting from the right, and each position that is 10 or greater, subtract 10 from that entry and add 1 to the entry to the left... watching out for a final carry that expanded the number of digits.
Once you have the vector of decimal values, having carried out the carries, add back '0' and convert to char()
댓글 수: 7
Rik
2021년 2월 15일
Your problem is that you didn't define the addition yourself, but didn't make sure to have an integer conversion either.
x = randi([0,9],1,30);
y = randi([0,9],1,30);
x_new = str2double(sprintf('%d',x));
y_new = str2double(sprintf('%d',y));
result = num2str(x_new+y_new)
As you can see, num2str reverts to an exponential notation, so you will have to switch to something else:
result = sprintf('%.0f',x_new+y_new)
Walter Roberson
2021년 2월 16일
Can you please modify my code as my code works so fine when a choose:
I already described what to do. You should revise your code nearly completely. I will not write this code for you, as you are doing a homework assignment.
I suggest you subtract '0' from each of the characters, line the two up so that the right side matches and zero-pad on the left if need be. Now add, getting a vector of decimal values. Now run through the vector starting from the right, and each position that is 10 or greater, subtract 10 from that entry and add 1 to the entry to the left... watching out for a final carry that expanded the number of digits.
Once you have the vector of decimal values, having carried out the carries, add back '0' and convert to char()
참고 항목
카테고리
Help Center 및 File Exchange에서 Numeric Types에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!