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
古いコメントを表示
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
採用された回答
その他の回答 (4 件)
Guillaume
2014 年 9 月 20 日
n = 1994;
num2str(n) - '0'
6 件のコメント
tyler brecht
2014 年 9 月 22 日
CHAIYUT CHAROENPHON
2016 年 7 月 16 日
work perfectly.
HARSHA HN
2019 年 7 月 11 日
Legendary answer!
Vo Luan
2021 年 4 月 22 日
Best answer, Thank you!
andrea
2021 年 6 月 16 日
Can anyone give me an hint on why it works ? thank you
Jan
2021 年 6 月 16 日
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]
N = 1994;
m = floor(log10(N));
D = mod(floor(N ./ 10 .^ (m:-1:0)), 10);
>> D = [1, 9, 9, 4]
2 件のコメント
Tejas Mahajan
2020 年 10 月 9 日
Hi Jan,
Can you please explain this code please.
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
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 件のコメント
Guillaume
2014 年 9 月 20 日
Or my solution is a lot more straightforward
sum(num2str(n)-'0');
per isakson
2014 年 9 月 20 日
編集済み: per isakson
2014 年 9 月 20 日
Straightforward to those who
- are familiar with ascii-numbers
- understand that Matlab automagically returns numbers as the result of a difference between two strings (minus is defined for strings?)
John
2014 年 9 月 20 日
This would make a great MATLAB Cody challenge
Guillaume
2014 年 9 月 20 日
Oh, it's been used in a lot of cody challenges.
per isakson
2014 年 9 月 20 日
Guillaume
2014 年 9 月 20 日
Well, as the accepted answer states, it's the most efficient way of converting a string into individual numbers, and the way you'd do it in a number of other languages (at least the C based one).
per isakson
2014 年 9 月 20 日
編集済み: per isakson
2014 年 9 月 20 日
tyler brecht
2014 年 9 月 20 日
Star Strider
2014 年 9 月 20 日
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
2017 年 4 月 12 日
編集済み: Joachim Posselt
2017 年 4 月 12 日
0 投票
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
カテゴリ
ヘルプ センター および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!