how to find non integer elemets in matrix?

조회 수: 10 (최근 30일)
Lidor Levi
Lidor Levi 2018년 9월 13일
댓글: Star Strider 2018년 9월 13일
hello
I have a matrix and I would like to check each element if its a whole number (integer) and if its not an integer I would like to copy those elmenets into diffrent 1 row matrix
with out using loops
thank you

채택된 답변

Star Strider
Star Strider 2018년 9월 13일
Try this:
A = randi(9, 4); % Integer Array
idx = randperm(16,5);
A(idx) = A(idx)+rand(1,5) % Some Non-Integer Elements
NotInteger = A(rem(A,1) ~= 0)
producing:
A =
6.0000 3.0000 2.1174 9.0000
2.0000 8.0000 3.5079 4.0000
3.0000 2.0000 4.3188 2.2967
5.0000 3.4242 3.0000 9.0000
NotInteger =
3.4242
2.1174
3.5079
4.3188
2.2967
It works because the rem (or mod) of any number with the denominator of 1 produces the fractional part of the argument. If that is zero, the element is an integer.
  댓글 수: 2
madhan ravi
madhan ravi 2018년 9월 13일
+1
Star Strider
Star Strider 2018년 9월 13일
@madhan —
Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by