enter a string of characters and numbers, add and return the sum of the numbers in matlab, for example: 'there are two blue birds and 4 red birds on the branch'--> return 6
4 ビュー (過去 30 日間)
古いコメントを表示
please help me solve this problem: enter a string of characters and numbers, add and return the sum of the numbers in matlab, for example: 'there are two blue birds and 4 red birds on the branch'--> return 6
2 件のコメント
Walter Roberson
2021 年 9 月 3 日
Which names of numbers need to be handled? For example does it need to be able to handle,
there are seventeen thousand million three hundred and sixty two thousand five hundred and nine red birds
回答 (1 件)
Konrad
2021 年 9 月 3 日
Hi
if you only need to handle numeric type, I'd recommend regular expressions:
strng = ' 2 blue birds and 4 red birds';
res_str = regexp(strng,'\d+(\.\d+)?','match');
res_num = str2double(res_str);
res_sum = sum(res_num);
If you also want to handle written numbers as in your initial example
'there are two blue birds and 4 red birds on the branch'
this FEX might by of great help.
Best, Konrad
3 件のコメント
Walter Roberson
2021 年 9 月 3 日
The regexp needs a bit of adjustment if you want to handle numbers with leading period such as .75
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!