-1

Would there be a function in matlab, or an easy way, to generate the quantile groups to which each data point belongs to?

Example:

x = [4 0.5 3 5 1.2];
q = quantile(x, 3);

ans =

    1.0250    3.0000    4.2500

So I would like to see the following:

result = [2 1 2 3 1]; % The quantile groups

In other words, I am looking for the equivalent of this thread in matlab

Thanks!

Community
  • 1
  • 1
Mayou
  • 8,498
  • 16
  • 59
  • 98

2 Answers2

0

You can go through all n quantiles in a loop and use logical indexing to find the quantile

n = 3;
q = quantile(x,n);
y = ones(size(x));
for k=2:n
    y(x>=q(k)) = k;
end
hbaderts
  • 14,136
  • 4
  • 41
  • 48
0

Depending on how you define "quantile group", you could use:

  1. If "quantile group" means how many values in q are less than x:

    result = sum(bsxfun(@gt, x(:).', q(:)));
    
  2. If "quantile group" means how many values in q are less than or equal to x:

    result = sum(bsxfun(@ge, x(:).', q(:)));
    
  3. If "quantile group" means index of the value in q which is closest to each value in x:

    [~, result] = min(abs(bsxfun(@minus, x(:).', q(:))));
    

None of these returns the result given in your example, though: the first gives [2 0 1 3 1], the second [2 0 2 3 1], the third [3 1 2 3 1].

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147