lundi 15 février 2021

Addition of numbers in array as string in MATLAB

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 output 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




Aucun commentaire:

Enregistrer un commentaire