1

I want to see whether a column X in a table T is in sorted order. class(x) is double. I compare X to sort(x) using isequal and find that the answer is false.

I visually inspect, and find no entries that differ. Further, by making a table [T.x sort(T.x)], I see that the first 2011 entries are equal to each other. The 2012th row is some how not equal, using the isequal function. Here is a dput of this row

ans =

reshape([2080857.000000 2080857.000000 ],[1  2])

Converting to int64 also does not give equality.

This is not the same as the linked duplicate question. I have tried techniques from there, including format long g and my values are still the same. In addition, the two columns of the table are the 'same', one is just the sorted ordering of the other, and to isequal, the first 2011 entries are equal and the 2012th entry is equal to my naked eye, or converted to integer.

Alex
  • 15,186
  • 15
  • 73
  • 127
  • I am not a MatLab expert, but **isequal(A,B) returns logical 1 (true) if A and B are the same size and their contents are of equal value**. Isequal don't care about the type, it checks for array size and equality of elements at same index – Bartłomiej Sobieszek Aug 18 '15 at 04:24
  • I'm curious. If you repeat this, do you always end up with the same column that gives you problem? – user3667217 Aug 18 '15 at 04:54
  • what should I repeat, sorry? – Alex Aug 18 '15 at 05:05
  • sort . Try sorting X for a few times and see if you produce the same error ? – user3667217 Aug 18 '15 at 05:09
  • no, it doesn't give the same answer! i.e. using `temp_ans = sort(X)` `temp_ans2 = sort(temp_ans)`, gives true for `isequal(X(1:2012), temp_ans2(1:2012))`. Also, true for `isequal(temp_ans, temp_ans2)` – Alex Aug 18 '15 at 05:14
  • try `any(isnan(X(:)))` and check if there is any NaN in your matrix. If there is any NaN, `isequal` will return false. – user3667217 Aug 18 '15 at 05:20
  • there is no NaN in my matrix. Definitely no NaN in the first 2012 rows, as the first 2011 rows have equality. – Alex Aug 18 '15 at 05:21
  • what does X(2012) - sort(X)(2012) give? It is no surprise that int64 makes them equal as int64 rounds to closest integer and even if they are not equal they are obviously close enough – alexandre iolov Aug 18 '15 at 08:40
  • 1
    Misread the question. I thought you were trying to compare floating point numbers together. Reopening. – rayryeng Aug 18 '15 at 12:51
  • So far it's impossible for any of us to replicate this without the actual data. I don't trust your use of `dput` (and I have no idea if the tool is any good). It looks like you may be using `dput`'s default precision rather than whatever would be best to compare the full double precision value. Just subtract the two columns from each other and you'll probably see that some elements differ by very small values (likely on the order of `eps(x)`). – horchler Aug 18 '15 at 14:42
  • @horchler I understand your concern. But could you think of any reasons why after conversion to `int64` the numbers are still not equal? – Alex Aug 18 '15 at 23:11
  • 1
    Sure: `a=2080857.5;` `b=[a a-eps(a)],` `int64(b)`. On inspection the the numbers in `b` will appear identical (even with `format long`), but they're not equal and `int64(b)` will produce two different integers. If you want to see a complete string representation use `sprintf('%.17f\n ',b)` or `sprintf('%.17g\n',b)`. – horchler Aug 18 '15 at 23:33
  • I see why now, since `int64` does not actually do truncation. Is there a `int` function that does truncation? – Alex Aug 18 '15 at 23:37
  • 1
    But your counter example is contrived to make `a - eps(a)` fall on the wrong side of `0.5`. If we use: `a=2080857.4; b=[a a-eps(a)]; int64(b)`, then conversion to `int64` should give the same integer right? – Alex Aug 18 '15 at 23:38
  • Of course it's contrived. Check your actual data to see what is going on. We can't help you more with that without having it ourselves. If you want Matlab integers to behave like C ones you're in for [a bunch of work](http://stackoverflow.com/questions/2425251/how-do-i-get-real-integer-overflows-in-matlab-octave). If you just want truncation use something like `fix`. – horchler Aug 18 '15 at 23:43
  • Also, to address the `dput` comment, I don't know how else I should export this data? Should I save the line as a .mat file and link to it? – Alex Aug 18 '15 at 23:45
  • 1
    A binary MAT-file should preserve the values and behavior. You should confirm that you can replicate your issue (and provide the code you use) with the values in this file before posting. And again, just subtract the columns from each other to see which values are different. – horchler Aug 18 '15 at 23:52
  • You could also look at `x(1) - x(2)` and see whether the result is 0. Most likely, the two numbers are not identical, even if only by a tiny amount. – A. Donda Aug 19 '15 at 02:13

0 Answers0