How to Add the Individual digits of a result ?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Shravankumar P
le 28 Juin 2014
Modifié(e) : Star Strider
le 28 Juin 2014
>> 789*4567
ans =
3603363
I want to add each element of the ans :
i.e., >> 3+6+0+3+3+6+3
ans =
24
Help me how to do this?
0 commentaires
Réponse acceptée
dpb
le 28 Juin 2014
Lots of ways...just a couple--
>> sum(str2num(num2str(789*4567).'))
ans =
24
>> >> sum(sscanf(num2str(789*4567),'%1d'))
ans =
24
>>
0 commentaires
Plus de réponses (1)
Image Analyst
le 28 Juin 2014
Try this:
theNumber = 3603363
theString = num2str(theNumber)
theSum = sum(theString - '0')
(I hope I didn't just do your homework.)
0 commentaires
Voir également
Catégories
En savoir plus sur Crystals dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!