How can I get different elements of a vector?

5 Ansichten (letzte 30 Tage)
Jorge
Jorge am 21 Mai 2014
Kommentiert: Matt J am 21 Mai 2014
For example, I have:
a = [3 5 5 9 5 3 9 5];
I need for answer:
3 5 9 % different elements in the vector
1 6 % index of first element
2 3 5 8 % index of second element
4 7 % index of third element
Help me!
Thank

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 21 Mai 2014
Bearbeitet: Azzi Abdelmalek am 21 Mai 2014
a = [3 5 5 9 5 3 9 5];
[ii,jj,kk]=unique(a);
idx=accumarray(kk,1:numel(kk),[],@(x) {sort(x)'});
disp(ii)
celldisp(idx)
  3 Kommentare
Sean de Wolski
Sean de Wolski am 21 Mai 2014
But is that undocumented, I wonder
Matt J
Matt J am 21 Mai 2014
(and super useful :)
Indeed!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by