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

댓글 수: 4

Birdman
Birdman 2018년 2월 1일
What is your question?
F K
F K 2018년 2월 1일
편집: F K 2018년 2월 1일
I don't know how code properly another loop into this code to sum down the sum.
I guess my question is, "can someone help me find a solution?".
Birdman
Birdman 2018년 2월 1일
I still do not understand. Give a numeric example.
F K
F K 2018년 2월 1일
E.g. num = 123456789
The function example code I used results in s = 45
I would like to program the result of s down to one digit, s = 9

댓글을 달려면 로그인하십시오.

 채택된 답변

Jan
Jan 2018년 2월 2일
편집: Jan 2018년 2월 2일

1 개 추천

num = 123456789;
while num > 9
dec = 10 .^ (0:ceil(log10(num)) - 1);
digits = rem(floor(num ./ dec), 10);
num = sum(digits);
end
disp(num)
The conversion from the number to the digits is done inside sprintf also, but this performs an additional conversion to a char vector. I prefer to stay at the original data type, although it is nice to hide the actual calculations inside the built-in sprintf.
I hope that this is not your homework. Otherwise it gets harder to submit your own version to avoid "cheating".
Based on your own method all you need is an additional outer loop:
num = 123456789;
while num > 9
s = 0;
while num >= 1
s = s + rem(num, 10);
num = floor(num / 10);
end
num = s;
end
disp(num)

댓글 수: 1

F K
F K 2018년 2월 2일
Thank you Jan, and All who helped!
It's a hobby i am trying to accomplish, i wish a teacher would have given me homework about this ^_^
i just started writing here and love you guys already...unfortunately i'm super slow, so step by step ,-)

댓글을 달려면 로그인하십시오.

추가 답변 (5개)

Image Analyst
Image Analyst 2018년 2월 1일

2 개 추천

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

F K
F K 2018년 2월 1일
Your answer is great for a trick and looks elegant, unfortunately it gives 45 as result.
Num can be a very big number and I want to reduce it down to only one digit.

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2018년 2월 1일

1 개 추천

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

F K
F K 2018년 2월 1일
Thank you for explaining the concept

댓글을 달려면 로그인하십시오.

Birdman
Birdman 2018년 2월 1일
편집: Birdman 2018년 2월 1일

1 개 추천

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

댓글 수: 3

F K
F K 2018년 2월 1일
편집: F K 2018년 2월 1일
Your solution doesn't show me the answer. I tried to display each variable..
Birdman
Birdman 2018년 2월 2일
Delete semicolons and then run again
Jan
Jan 2018년 2월 2일
Using numel(num2str(s)) is a very indirect way of s < 10 . numel(num2str(s)) could be expressed directly by floor(log10(s)) + 1. Even sprintf would have less overhead as num2str.

댓글을 달려면 로그인하십시오.

F K
F K 2018년 2월 1일
편집: F K 2018년 2월 1일

0 개 추천

Wow that's a beast... Are you converting to string to get a length property?
Thank you so much !
Edit* I need to think a bit which solution I like to accept. All are right ;-)

댓글 수: 5

Image Analyst
Image Analyst 2018년 2월 1일
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
F K 2018년 6월 28일
how can i contact you ?
Image Analyst
Image Analyst 2018년 6월 28일
You just did.
F K
F K 2018년 6월 28일
^_^ i would like to hire you for a private project which i cant discuss here. Can you turn the contact settings from this forum on so i can send you details ?
Jan
Jan 2018년 6월 28일
@F K: What about enabling your contact settings in your profile?

댓글을 달려면 로그인하십시오.

F K
F K 2018년 2월 1일

0 개 추천

I extended your code 'Image Analyst' to show how this functions should return only one digit no matter how big num is.
clc
num = 123456789123456789;
digits = num2str(num) - '0';
s = 0;
t = 0;
u = 0;
for k=1:length(digits)
s = s + digits(k);
end
digits2 = num2str(s) - '0';
for k=1:length(digits2)
t = t + digits2(k);
end
digits3 = num2str(t) - '0';
for k=1:length(digits3)
u = u + digits3(k);
end
s
t
u
result
s =
128
t =
11
u =
2

댓글 수: 11

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)
F K
F K 2018년 2월 2일
편집: F K 2018년 2월 2일
Thank you for this suggestion. I used 123456789123456789 only as example to show, how the first iteration sums it down to 128.
The function i'm trying to program needs to sum all numbers in a 3x3 matrix down to "*one digit*" that means.
The result must be a digit from 1 - 9 .
Image Analyst
Image Analyst 2018년 2월 2일
편집: Image Analyst 2018년 2월 2일
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
Walter Roberson 2018년 2월 2일
Yah, so, use the algorithm I told you instead.
F K
F K 2018년 2월 2일
편집: F K 2018년 2월 2일
the matrix holds each of the 1 - 9 numbers like a magic square.
% (+) (-) (+/-)
% 1 2 3
% 4 5 6
% 7 8 9
the positive group holds 1 4 7
the negative group holds 2 5 8
the neutral group holds 3 6 9
..in essence, i didn't come to this part yet
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(:));
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
F K 2018년 2월 2일
I think I found a solution here. Just have to test in. https://www.geeksforgeeks.org/finding-sum-of-digits-of-a-number-until-sum-becomes-single-digit/
Image Analyst
Image Analyst 2018년 2월 2일
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
F K 2018년 2월 2일
Yes indeed. You all were very helpful with my learning curve. Thank you very much.
I have to write more details about the project I am working on and will share more info if requested. This is unfortunately just a fraction of the things which have to be implemented.
Walter Roberson
Walter Roberson 2018년 2월 2일
Perhaps you should just take the number mod 9 (except using 9 instead of 0 for exact multiples): the results will be the same.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

F K
2018년 2월 1일

댓글:

Jan
2018년 6월 28일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by