I'm looking an efficient way to turn the vector:
[1,1,1,2,3,3,3,4,4,4,5,1]
into a vector of vectors such that:
[[1,2,3,12],[4],[5,6,7],[8,9,10],[11]]
In general:
newVector[i] = indexes of the initial vector that contained i
Preferably in Matlab/Octave but I'm just curious if there is an efficient way of achieving this.
I tried looking it up on google and stack but I have no idea what to call this 'operation' so nothing came up.
There is an easy way to do it using accumarray
A = [1,1,1,2,3,3,3,4,4,4,5,1]
accumarray(A',A',[],@(x){find(ismember(A,x))})
But next time, please show your own attempt in your question
Alternatively (but only if A
starts from 1
and doesn't skip any numbers)
accumarray(A', (1:size(A,2))', [], @(x){sort(x)})
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments