Sum down to one digit
Afficher commentaires plus anciens
I am trying to Sum numbers in a matrix down to one digit.
I am using this code
>> tic,s=0; while num>=1, s=s+rem(num, 10); num = floor(num / 10); end,toc,s
Elapsed time is 0.000010 seconds.
s =
78
I don't know how code properly another loop into this code to sum down the sum.
Can someone help me find a solution and explain it, if possible?
Thanks for helping
Réponse acceptée
Plus de réponses (5)
Image Analyst
le 1 Fév 2018
Here's another way, using the string trick:
num = 123456789
digits = num2str(num) - '0';
s = 0;
for k = 1 : length(digits)
s = s + digits(k);
end
s % Print to command window
1 commentaire
F K
le 1 Fév 2018
Walter Roberson
le 1 Fév 2018
There are numerous approaches. One of them is
while num > 9
break num up into last digits, and num without the last digit
replace num with the sum of that last digit and the number without the last digit
end
Using mod() to get the last digit is fine.
1 commentaire
F K
le 1 Fév 2018
num=123456789;s=0;
while num>0
s=s+mod(s,10);
num=floor(num/10);
end
while numel(num2str(s))~=1
s=floor(s/10^(numel(num2str(s))-1))+mod(s,10^(numel(num2str(s))-1));
end
5 commentaires
Image Analyst
le 1 Fév 2018
No, I converted to a string to eliminate the complicated stuff about using log(), floor(), rem(), mod(), powers of 10, etc. Basically the string trick converts a single multi-digit number into an array where each element of the array is one single number. Then all you do is simply add up the array! Can't get much simpler than that.
F K
le 28 Juin 2018
Image Analyst
le 28 Juin 2018
You just did.
F K
le 28 Juin 2018
Jan
le 28 Juin 2018
@F K: What about enabling your contact settings in your profile?
F K
le 1 Fév 2018
11 commentaires
Walter Roberson
le 1 Fév 2018
MATLAB cannot store 123456789123456789 as a double precision number. The value stored would be 123456789123456784 instead.
MATLAB can store 123456789123456789 as uint64:
num = uint64(123456789123456789)
Image Analyst
le 2 Fév 2018
Modifié(e) : Image Analyst
le 2 Fév 2018
Tell us the 3 by 3 matrix. All I see is one long number. What are the other 8 numbers? Where do they come from?
Walter Roberson
le 2 Fév 2018
Yah, so, use the algorithm I told you instead.
Image Analyst
le 2 Fév 2018
OK, I'm lost. Then if your numbers are actually in a 3x3 matrix instead of as one long number, why don't you simply do
s = sum(your3x3Matrix(:));
Walter Roberson
le 2 Fév 2018
function r = sum_to_one_digit(V)
if isempty(V)
r = 0;
elseif length(V) > 1
r = sum_to_one_digit(sum(V));
else
... code that only has to worry about working with a single number
end
F K
le 2 Fév 2018
Image Analyst
le 2 Fév 2018
It's just a more complicated version of what I gave you, and it's the same as what the others told you. But it doesn't have anything to do with a 3x3 matrix.
F K
le 2 Fév 2018
Walter Roberson
le 2 Fév 2018
Perhaps you should just take the number mod 9 (except using 9 instead of 0 for exact multiples): the results will be the same.
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!