Check if the number has any digits after the decimal points matlab
109 次查看(过去 30 天)
显示 更早的评论
I have a vector which is a mixture of floating point and whole numbers. I am trying to identify the whole numbers and divide them by 100. Assuming the vector is 'v'
v = [9.3, 6.8, 7, 4.1, 3]
I want to identify 7 and 3. How do I go about this, most of the algorithms I came across on mathworks do not work for a vector or they only return the number of digits after the decimal and if its a whole number i get error.
0 个评论
采纳的回答
Star Strider
2014-9-14
编辑:Star Strider
2014-9-14
This works:
v = [9.3, 6.8, 7, 4.1, 3];
v100 = v(mod(v,1)==0)*100
produces:
v100 =
700 300
It takes advantage of mod(v,1) yielding the part of any element of v to the right of the decimal. If that is zero (it creates a logical subscript vector where those values are true=1), it multiplies that element by 100 and assigns it to v100.
1 个评论
Roger Stafford
2014-9-14
You can also use any one of these three: round(v)==v, ceil(v==v, or floor(v)==v in the above code.
更多回答(1 个)
Ron Aditya
2014-9-15
1 个评论
Star Strider
2014-9-15
My pleasure!
I was under the impression your vector is a numeric array. I didn’t realise it was a string array or cell array of strings. Both Roger’s and my approaches use ‘logical indexing’.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!