Hi, I am trying to use grpstats on my program and trying to compute median and percentile and skew but somehow i am not getting the program to run.

This is what I write
[ Z]= [WeekNum,C,E,G,I];
[stat1,stat2]= grpstats(Z,WeekNum,{'mean',@(Z) prctile(Z,50)})
This is the error I am getting---
Error using grpstats>tryeval (line 429) Error computing statistics for group '10'.
Error in grpstats (line 304) z(gnum,:,:) = tryeval(hfun,x(idx,:),glabel{gnum},tsize)';
Error in portfoliocharacterization (line 24) [stat1,stat2]= grpstats(Z,WeekNum,{'mean',@(Z) prctile(Z,50)}) Caused by: Function '@(Z)prctile(Z,50)' returned a result of size [1 1], expected size [1 5]. ------------------------------------------------------------------------
My data has 5 columns,containing financial statistics and I am grouping them based on what week they were calculated.
I would appreciate any help.

 채택된 답변

Rishav, I suspect the problem is that your data has a weeknum for which there is only one row. You'll need to specify 1 for the DIM argument to PRCTILE in your anonymous function. GRPSTATS tries to save time by evaluating the functions on all columns or a group of rows all at once, but when it hits the group with one row, PRCTILE without DIM computes along that row, not down the columns, and returns a scalar rather than a 1x5.
All of these summary stat functions have a DIM argument to control just this kind of edge case, it's usually a good idea to use them if the input can be a matrix. In this case, it's probably not obvious that GRPSTATS is passing a matrix to your function, but it is.
BTW, there is a median function, so you could have just used @(x)median(x,1).

추가 답변 (1개)

Wayne King
Wayne King 2013년 8월 2일
편집: Wayne King 2013년 8월 2일
Hi Rishav, WeekNum should be your grouping variable for the observations in Z so I'm not sure why you are including WeekNum in the matrix Z. For example, suppose I have the following temperature data with month as the grouping variable
month = [1 1 1 1 6 6 6 6 9 9 9 9]';
% then the temperature data
temp_jan = [20 15 21 17]';
temp_jun = [75 78 80 82]';
temp_sept = [65 60 70 75]';
temp_data = [temp_jan temp_jun temp_sept];
temp_data = reshape(temp_data,12,1);
[stat1,stat2]= grpstats(temp_data,month,{'mean',@(temp_data) prctile(temp_data,50)});

카테고리

도움말 센터File Exchange에서 Repeated Measures and MANOVA에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by