Matlab - a command to sort variables into groups and output the group indices

后端 未结 2 1840
眼角桃花
眼角桃花 2021-01-25 04:20

I have a vector, for example: a = [1 1 2 2 7 7 7 10 10 10 10 11 15]. It can be unsorted, but here I\'m writing it as sorted. I am looking for a Matlab command that will convert

相关标签:
2条回答
  • 2021-01-25 04:34

    I'm not sure if it's possible to do the conversion with only one command, but this is one way to do it:

    a = [1 1 2 2 7 7 7 10 10 10 10 11 15];
    
    a = sort(a);
    UniqVector = unique(a);
    for i = 1:size(UniqVector, 2)
        a(a == UniqVector(i)) = i;
    end
    
    0 讨论(0)
  • 2021-01-25 04:47

    There's at least two ways to do this

    (1) use the third output of unique:

    [~,~,out] = unique(a)
    

    (2) use grp2idx from the statistics toolbox

    out = grp2idx(a)
    
    0 讨论(0)
提交回复
热议问题