How can I get different elements of a vector?

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

 Accepted Answer

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 Comments

Interesting that accumarray can return type 'cell'. But is that undocumented, I wonder???
(and super useful :)
Indeed!

Sign in to comment.

More Answers (0)

Asked:

on 21 May 2014

Commented:

on 21 May 2014

Community Treasure Hunt

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

Start Hunting!