Rosa
Rosa

Reputation: 253

Unique function in MATLAB?

I used unique (MATLAB function ) for finding unique rows of the matrix.

The matrix constructed in a function and I use unique after that. But the answer is not true.

unique just change matrix rows. By format long in MATLAB, the rows are equal.

I really do not know what is the problem? Am I wrong?

I am grateful to you for your help.


EDIT From the comment on an answer:

Actaully my matrix is the vertices of the quadrilateral, that sometimes be a line. for example:

A=[0.3 0.4;0.3,0.4;0.4,0.3;0.4,0.3] 

and by unique

A=[0.4,0.3;0.4,0.3;0.3,0.4,0.3,0.4] 

but I need to

A=[0.4,0.3;0.3,0.4]

Upvotes: 0

Views: 569

Answers (2)

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21561

Following your example, I get exactly what you would expect.

clear
A=[0.3 0.4;0.3,0.4;0.4,0.3;0.4,0.3];
A = unique(A,'rows')

Gives

A =

    0.3000    0.4000
    0.4000    0.3000

Either you call unique wrongly, or the rows are not exact duplicates.

To check for the latter, try calculating the difference between two 'equal' rows and see whether it returns zeros.

Upvotes: 1

Chris Taylor
Chris Taylor

Reputation: 47402

Could this be a floating point issue? If two numbers are very close, they may appear to be equal when you display them (with or without using format long) even though they are not actually equal. For example

>> X = [1, 1e-20, 1e-20; 1, 1e-20, 1e-21];
>> format long;
>> X
ans =
   1.000000000000000   0.000000000000000   0.000000000000000
   1.000000000000000   0.000000000000000   0.000000000000000

So the rows look equal. However, when you do

>> unique(X, 'rows')
ans =
   1.000000000000000   0.000000000000000   0.000000000000000
   1.000000000000000   0.000000000000000   0.000000000000000

You can see that they aren't equal.

Upvotes: 0

Related Questions