Clear Filters
Clear Filters

apply function to each group

2 views (last 30 days)
Qifan
Qifan on 27 Jul 2014
Commented: Qifan on 27 Jul 2014
Can anyone tell me how to apply a function to each group data? For example, there are 100 firms, and each firm has 10 years total assets observations. So in total there are 1000 firm-year observations, with 10 observations each group. I need to apply a movingstd function to each group observations, simple grpstats function can not solve, because moving standard deviation need other inputs. Thanks for having a look.
  3 Comments
Image Analyst
Image Analyst on 27 Jul 2014
What is the definition of a "group"? Is that 10 observations/measurements of one firm over 10 years? Is it multiple firms? Is your data a double array of 100 rows by 10 columns? If you want "moving" stats, that implies that there is some window that moves along, so you're not just taking the mean of all 10 observations, but of some subset of it in a window as the window moves along. What is the window width. How do you want to handle the edge effects when the center of the window is such that some of the window would be outside your array?
Qifan
Qifan on 27 Jul 2014
My data is in dataset. For example, the total asset is one variable (one column). Suppose it has 100 different firms with 10 year obs for each. So total asset variable, the column, will have 1000 rows. Now I have the rolling standard deviation function already, the problem is I need to apply this function to each firms data (each firm can be viewed as a group since it contains 10 obs). Thanks.

Sign in to comment.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 27 Jul 2014
If your data are cell array, you can use cellfun
  1 Comment
Qifan
Qifan on 27 Jul 2014
My data is a dataset, and can be easily tranformed into a matrix. I'll have a look of this function.Thanks.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 27 Jul 2014
Extract out one column of 1000, then reshape into 100 rows by 10 columns
newMatrix = reshape(oneColumn, [100, 10]);
Each column will now be just one firm, not multiple firms. Then use a for loop to go down each column.
I still don't know the window width of the function that takes the standard deviation. We can "roll" that down a row at a time to get a moving/rolling average but how do you want to handle the case where the window is outside the column? Like if the window is 5 wide but the center of the window is at row #1. Or do you want to just start and end inside so that the window does not leave your data?
  1 Comment
Qifan
Qifan on 27 Jul 2014
Thanks, let me try reshape. The window width is 5.

Sign in to comment.

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!