Check if the number has any digits after the decimal points matlab

조회 수: 111 (최근 30일)
Ron Aditya
Ron Aditya 2014년 9월 14일
댓글: Star Strider 2014년 9월 15일
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.

채택된 답변

Star Strider
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
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
Ron Aditya 2014년 9월 15일
I ended up using this:
for i=1:l x(i)=str2double([b{i+1,1}{1,1}]); if rem(x(i),1) == 0 x(i)=x(i)/100; end end
Thanks though!
  댓글 수: 1
Star Strider
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 CenterFile 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!

Translated by