Average returns for diffferent companies
2 次查看(过去 30 天)
显示 更早的评论
Good afternoon,
I have the following set of data:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/159589/image.png)
I want matlab to calculate the average of column 4, dependent on the values of column 3 (which are company tickers). That is, I want matlab to compute average returns of column 4 while column 3 refers to the same company (from row 1 to row 9 in this case).
Given that I have different number of observations in column 3 for each company (some have more data than others), is there a simple way to automatize this? For instance, to tell matlab that when Column 3, row i+1 == row i, do the average of the values of column 4 for this range.
I am new at matlab and any help would be much appreciated. Thank you!
0 个评论
回答(1 个)
Jos (10584)
2014-3-27
Assuming DATA is a cell array holding your data, something along these lines could work:
[COMPANY,~,idx] = unique(DATA(:,3))
AVG = accumarray(idx, [DATA{:,4}],@mean)
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!