how can I check if a specific value in a matrix is an integer?

조회 수: 42 (최근 30일)
Seba.V
Seba.V 2019년 8월 6일
댓글: Adam Danz 2019년 8월 7일
for i=14:20
A=[1 1 1; 10 6 2;0 1 0];
B=[50; 360; i];
C=A^-1*B;
y=C(1,1);
x=C(3,1);
if y/round(y)==1 && x/round(x)
fprintf('%3g & %3g integers\n',y,x)
else
fprintf('%3g & %3g not intigers\n',y,x)
end
end
by input different values of i am trying to see if the values of x and y are integers.
The output from the following code gives me the correct answer only to the first integer value checked and then fails all others.
How do I make the loop continue to check correctly on the other values generated?
  댓글 수: 2
Bryce Johnson
Bryce Johnson 2019년 8월 6일
Try using logical arrays instead and use the isinteger(Matrix) it will return a logical matrix and then you can use the any and all functions to see if they are integers? A little unclear as to what you are asking.
Adam Danz
Adam Danz 2019년 8월 6일
편집: Adam Danz 2019년 8월 6일
Note that isinteger() detects values that are of the integer class such as int8, int16, int32, int64, uint8, uint16, uint32, and uint64. It does not test that a value is an integer (example below).
isinteger(1.0)
ans =
logical
0

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

답변 (2개)

Adam Danz
Adam Danz 2019년 8월 6일
편집: Adam Danz 2019년 8월 6일
There are better ways to test for integers. Your method is susceptible to round-off error. It also appears that you forgot the "==1" following round(x) which is why it's not working. Instead, use this method to test for intergers.
if mod(y,1) == 0 && mod(x,1) == 0
% or
if all(mod([x,y],1) == 0)
I also suggest changing the fprintf() commands so you can see many more decimal places.
fprintf('%.20f & %.20f not integers\n',y,x)
  댓글 수: 4
John D'Errico
John D'Errico 2019년 8월 6일
편집: John D'Errico 2019년 8월 6일
True.
inf == round(inf)
ans =
logical
1
Is infinity an integer? ;-)
Note that mod also has a possibly subtle issue though.
x = rand()*1e60;
>> mod(x,1)
ans =
0
>> mod(x,1) == 0
ans =
logical
1
Is x there an integer? Arguably, it is, but which integer?
eps(x)
ans =
1.115e+43
Adam Danz
Adam Danz 2019년 8월 7일
@John D'Errico, I bet that'd be an interesting conversation!

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


Andreas Bernatzky
Andreas Bernatzky 2019년 8월 6일
편집: Andreas Bernatzky 2019년 8월 6일
I am not quiet sure if you mean a REAL integer or just a number like 3. Because matlab stores everything as double by default. But in your code you just use "default" numbers (double for matlab). So your questions does not make really sense to me but i interpret that you will just check for example if a number is 14.0 or 14.3 (as example).
If you mean real integer here is a exemple:
TF = isinteger(int8(2))
Classical approach for your problem: modulo-operator or as in matlab mod(number2check,divisor)
myNumber = 3;
rest = mod(myNumber,1); % myNumber /1
%if you can divide a number by 1 and it got no rest -> "integer"
if(rest ==0) %means it is an "integer"
disp('hey I am an integer');
else
disp('hey I am not an integer');
end
Especially for your example:
Vector2Check = [13,41,21.5,43,11.5]; % vector which includes all your number which shall get checked
for(a=1:1:length(Vector2Check))
rest = mod(Vector2Check(a),1);
if(rest ==0) %means it is an "integer"
disp('hey I am an integer');
else
disp('hey I am not an integer');
end
end
Hope I haven't completely misunderstood your question and I could help
Greets
Andi

카테고리

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