Unique not working - may be due to different datatypes in array

After smoothing out the data from one array I end up with one containing the values shown below. I want to get rid of the duplicate values 7 and 4.6 however unique will not get rid of them. Is this due to the fact that these values are different data types to each other? Any help would be appreciated.
Array
1.40000000000000
6.20000000000000
4.60000000000000
4.60000000000000
3.40000000000000
3.80000000000000
7.00000000000000
7
3.40000000000000
4
4.40000000000000
2.80000000000000
3.20000000000000

 채택된 답변

Walter Roberson
Walter Roberson 2019년 8월 19일

1 개 추천

Those are not different datatypes.
When you see the same array displaying 7.00000000000000 and 7 both, then what it tells you is that the 7 value is exactly the floating point representation of 7, but that the 7.00000000000000 value is slightly different from 7 exactly. If you were to take Array(7) - Array(8) you would see a very small difference, probably roughly 2E-15 . The value that is exactly 7.0000000000000017763568394002504646778106689453125 displays as 7.00000000000000 in some cases because the default formats do not display all of the digits.

댓글 수: 5

James Morris
James Morris 2019년 8월 19일
편집: James Morris 2019년 8월 19일
Ah I see, that makes sense!
Knowing that, I just did:
array = round(array,2);
array = unique(array);
Is that a good work around?
It looks plausible for your situation.
You're the man Walter, thank you.
Stephen23
Stephen23 2019년 8월 19일
편집: Stephen23 2019년 8월 19일
You could also try uniquetol, which was designed to handle this situation.
If any NaN values exist in rows, the unique function does not work (not eliminate parallel rows). I have this problem, so I converted NaN values to -99 using fillmissing, I use unique value again, and it's work.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

질문:

2019년 8월 19일

댓글:

BN
2021년 3월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by