apply function to each group
3 views (last 30 days)
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.
More Answers (1)
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?