Average returns for diffferent companies
Show older comments
Good afternoon,
I have the following set of data:

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!
Answers (1)
Jos (10584)
on 27 Mar 2014
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)
Categories
Find more on Entering Commands 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!