1

Say I had a row vector: A= [1,4,5.6]

and I wanted to have the following

if floor(A)~=A
error('vector contains non-integers')
else
*rest of program*
end

How would I need my code to be such that I can display the error message (and hence stop the program) if at least one of the elements in the row vector meets the if conditions?

1 Answers1

3

If ANY of them trigger the condition?

if any(floor(A)~=A)

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
  • @rahnema1 Yes and no. Any integer (to a certain value) can be represented perfectly, so I guess it depends on where the values come from. If they come from a text file, there is no problem with this code, if they come from a numerical method, then there may be problems. – Ander Biguri Dec 14 '16 at 14:36
  • 1
    anyway I prefer `if any (abs(A - floor(A)) > eps)` – rahnema1 Dec 14 '16 at 14:57
  • @rahnema1 I work with numerical algorithms, I agree with that. But there are cases where you are interested in having an integer `A`, not an `A` that is close to another number. – Ander Biguri Dec 14 '16 at 15:24
  • @rahnema1 http://stackoverflow.com/questions/40917167/find-a-number-before-another-specific-number-on-a-vector/40917241?noredirect=1#comment69046811_40917241 – beaker Dec 14 '16 at 15:46
  • @AnderBiguri So we are agree but since result of floor(A) is a double not an integer the comparison may become problematic – rahnema1 Dec 14 '16 at 17:12
  • 1
    @rahnema1 no, comparison between integer numbers (in float representation) is not a problem. Try `(0.1+0.2)==0.3` and `(1+2)==3` Change the numbers as pleased. Integers can be accurately represented in float and double to a very big number – Ander Biguri Dec 14 '16 at 17:29
  • @beaker I and you and Ander Biguri are agree although that question is slightly different. I learn from your Instructive comments – rahnema1 Dec 14 '16 at 18:22