How to use prctile in grpstats?

1 回表示 (過去 30 日間)
Lei
Lei 2013 年 5 月 15 日
Hello, I am using the grpstats function to get some statistic based on groups; however, I have trouble to use prctile for the whichstats. As the prctile requires a both input X and p. The p I want is [ 5 95]. But for the input x, I don't know how to handle with this as I have predefined X in the grpstats. Here is my grpstats input example:
[A,B,C,D,E,F] = grpstats(sample(:,2),sample(:,7),{'gname','numel','mean','var','skewness',@iqr});
Sample(:,2) is my input data, and sample(:,7) is the group. I am wondering how can I use the prctile in the grpstats properly. Thanks.

採用された回答

Tom Lane
Tom Lane 2013 年 5 月 16 日
The trick is to use an anonymous function that has the desired percentiles built in. For example, the following two calls to grpstats yield the same results:
x = randn(1000,2);
g = randi(5,1000,1);
grpstats(x,g, @median)
grpstats(x,g, @(x) prctile(x,50))
The following yields a 3-D array where the middle page, a(:,:,2), has the median and the other pages have the 5% and 90% points:
a = grpstats(x,g, @(x) prctile(x,[5 50 90]))
  3 件のコメント
Tom Lane
Tom Lane 2013 年 5 月 16 日
That's called an anonymous function. It's a way to define a function on the fly, at the command line, without entering code into a file. So "@(x)prctile(x,50)" is a function of x that is computed by invoking the prctile function with x as the first input and 50 as the second.
Lei
Lei 2013 年 5 月 16 日
I see. Thx!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeRepeated Measures and MANOVA についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by