Extract numbers from an integer
3 次查看(过去 30 天)
显示 更早的评论
I want to write code that allows the user to enter a number, for example 123411 and that matlab answers with how many ones the input contains, in this case 3. Can't seem to figure this out!
0 个评论
回答(4 个)
Jan
2017-11-12
This sounds like a homework question. The solution is not hard and it should be possible to solve. Unfortunately you did not post, what you have tried so far and did not ask a specific question. So how can we help you beside posting the working solution?
s = input('Type in the number: ', 's');
n1 = sum(s == '1')
And now? Can you submit this as your solution? Does it help you to learn Matlab if I post a solution?
Read the docs for nnz for a slightly different solution. And for the next time, please post you code (even if it is not working) and ask specific questions.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!