Hi guys. I need help splitting a number into its individual parts and then add them. E.g. the number would be 1994 = 1 + 9 + 9 + 4 = 23

71 次查看(过去 30 天)
I don't want the number to turn a scalar into an array eg x = 1994 = 1 9 9 4 I want it the scalar x = 1994 to split into multiple scalars. I hope this makes sense and your help will be much appreciated

采纳的回答

John
John 2014-9-20
编辑:John 2014-9-20
When you say you do not want the number to turn to a scalar, I think what you are saying is you don't want to convert the scalar number x = 1994 into a vector v = [1 9 9 4].
If you are allowed to use string functions and are loose on your restriction, consider this:
x = 1994
%Convert x = 1994 to a vector of characters
a = num2str(x); %a now holds ['1' '9' '9' '4']
%Go through each of the elements in the vector, a, convert them to numbers and add them up
sum = 0;
for i = 1:size(a, 2)
sum = sum + str2num(a(i));
end
%sum now contains the sum of the individual digits
But this seems like a typical undergraduate homework problem :-) for a comp. sci./electrical enginering course :-) and the string approach above is unlikely to be the proper solution. I hope no one else provides the mathematical solution to this :-) as this is not a MATLAB question.
As a hint, consider the significance of the position of the individual digits in the number. We call them the one's, ten's, hundred's, and thousand's position for a reason :-)
  2 个评论
tyler brecht
tyler brecht 2014-9-20
Wow u are wise :), thanks a lot John this is exactly what I was looking for:). I'm actually doing a mechanical engineering course, but this solution will help me a lot in completing my assignment, which contains a if function and while loop
John
John 2014-9-20
A glimpse from the mathematical vantage point:
x = 1994;
a = 1994 / 1000;
a = floor(a); %a = 1, ..cough.. the thousand's position
b = x - a * 1000; % b = 994
b = b / 100;
b = floor(b); % b = 9, ..cough.. the hundred's position
None of the variables above are vectors.

请先登录,再进行评论。

更多回答(4 个)

Guillaume
Guillaume 2014-9-20
n = 1994;
num2str(n) - '0'
  6 个评论
Jan
Jan 2021-6-16
@andrea: num2str converts a number to the corresponding CHAR vector:
n = 1994;
num2str(n) % '1994'
If you subtract another CHAR from this vector, this is done elementwise in Matlab:
'1994' - '0' % Is the same as:
['1', '9', '9', '4'] - '0'
% This is calculated elementwise:
['1' - '0', '9' - '0', '9' - '0', '4' - '0']
If CHARs appear as input to a numerical operation, their ASCII values are used. So this is converted to:
[49 - 48, 57 - 48, 57 - 48, 52 - 48]
which is:
[1, 9, 9, 4]

请先登录,再进行评论。


Jan
Jan 2019-7-11
编辑:Jan 2019-7-11
N = 1994;
m = floor(log10(N));
D = mod(floor(N ./ 10 .^ (m:-1:0)), 10);
>> D = [1, 9, 9, 4]
  2 个评论
John D'Errico
John D'Errico 2020-10-9
@Tejas Mahajan - easy enough. But you should make the effort yourself. What, for example does this do?
10 .^ (m:-1:0)
Now, what would happen when you do this?
N ./ 10 .^ (m:-1:0)
Now add one more layer around that?
floor(N ./ 10 .^ (m:-1:0))
Try that part for 1994. Now, look at the last step in his code.
mod(floor(N ./ 10 .^ (m:-1:0)), 10);
What did that do?
When you don't understand a piece of code, break it apart. Start in the middle, then work outwards, one part at a time until you do see what it does. You won't learn otherwise.

请先登录,再进行评论。


per isakson
per isakson 2014-9-20
编辑:per isakson 2014-9-20
A one-liner with a lot of Matlab
>> sum( arrayfun( @(a) str2double(a), num2str( 1994 ) ) )
ans =
23
or even more matlab-ish
>> sum( arrayfun( @str2double, num2str( 1994 ) ) )
ans =
23
This is essential the same as John's solution with the for-loop replaced by the function, arrayfun
  10 个评论
per isakson
per isakson 2014-9-20
编辑:per isakson 2016-7-17
Fewer lines of code is good, but one should only use code that one understands and is able to take responsibility for.

请先登录,再进行评论。


Joachim Posselt
Joachim Posselt 2017-4-12
编辑:Joachim Posselt 2017-4-12
yyyy = 1994;
% dig = digits extract // Ziffern extrahieren - mit Mathe, nicht mit Strings!!
dig_t = floor (yyyy / 1000); % tausender // thousands
dig_h = yyyy - dig_t * 1000;
dig_h = floor (dig_h / 100); % hunderter // hundreds
dig_z = yyyy - dig_t * 1000 - dig_h*100;
dig_z = floor (dig_z / 10); % zehner // ten
dig_e = yyyy - dig_t * 1000 - dig_h*100 -dig_z*10;
dig_e = floor (dig_e / 1); % einer // ones

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by