MATLAB: Grouping data by value

classification

I have a vector of data X that I want to divide into 10 different groups depending on the values. i,e the highest 10% together, then the lower 10% together etc…. How can I create another vector that assigns numbers to such groups. For example highest 10% should have a value of 1, then the lower 10% a value of 2 , … then the lowest 10% a value of 10. I used :
DecileThresholds= quantile(X,(1:9)/10);
to find the points that separates the categories but I don't want to write a loop to categorize them. I thought that matlab probably has a function that automatically and elegantly give me the category number. Thanks

Best Answer

x = [1, 8, 2, 3, 3, 7] ; % Dummy example.
[~, binId] = histc( x, [0, 4, 10] ) ; % Bin edges: 0 --bin1-- 4 --bin2-- 10
With that you get:
binId =
1 2 1 1 1 2
Then you can group them per bin:
grouped = accumarray( binId', x, [], @(v){v} ) ;
Related Question