What does the 'i' mean in [y,i] = unique(A)?

If I got a m x 1 matrix and I perform this little piece of code in Matlab it returns two arrays 'y' and 'i', where 'y' holds the unique numbers en 'i' holds the row numbers of the unique numbers? I don't understand why this is. Can someone explain this to me?

 Réponse acceptée

Star Strider
Star Strider le 28 Avr 2016
Modifié(e) : Star Strider le 28 Avr 2016

0 votes

It’s the index into ‘A’, so that ‘y=A(i)’. It returns the index of the first instance of the unique values. If you want all of them (indirectly), you have to ask for the third argument as well.

4 commentaires

Igor Batoukhtine
Igor Batoukhtine le 28 Avr 2016
Modifié(e) : Igor Batoukhtine le 28 Avr 2016
What's the difference between the first instance and all of them? I was reading on the vector index page, but I couldn't find this explained. Thanx for your first answer though.
Star Strider
Star Strider le 28 Avr 2016
My pleasure.
There is no difference if all of the values are unique, and all are already in ascending sorted order.
I don’t know what you intend by ‘the vector index page’. I would refer you to the documentation on the unique function, specifically Unique Values and Their Indices for the details.
Igor Batoukhtine
Igor Batoukhtine le 28 Avr 2016
Aaaah great, thanx! I was looking at the wrong page:
Star Strider
Star Strider le 28 Avr 2016
My pleasure!
That isn’t the ‘wrong’ page, and is definitely worth reading (I wish more posters here read it), but not the one that would provide the information you need to understand the unique function.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by