Average excluding values over 306x100 matrix
3 views (last 30 days)
Show older comments
Hello all,
I'm currently trying to calculate averages of stocks. The problem I currently have is that I have to exclude the value of the stock I´m looking at from the overall market average. As an example : If i have 40 stocks, for firm 1 I have to find the mean of stocks 2 through 40 and so on. So far I tried iterating through the rows ( different stocks) and moving indexes and then continue for each column, which represents the months.
My code so far is comprised by:
[a,p]=size(mon_innoilliq);
for m=1:a
for n=2:p;
marktdurchilliq(a, n) = nanmean( mon_innoilliq( 1 , [1:n , (n+1):p ] ), 2);
end
end
If I execute this, I get results for the first row, although the first average is incorrect, since I can't start indexing at 0 and for every other row I don't get any result at all. Also Matlab sometimes has problems with the dynamic indexing of the columns and the changing size of my result matrix.
I thank eyerone for a little help in this issue. As you can probably tell I'm pretty new to Matlab and appreciate every suggestion!
With kind regards.
2 Comments
Jan
on 9 Jun 2016
The description is not clear. The code does not define exactly, what you are looking for also. Not that the inner loop does not depend on the outer loop in any case. In addition [1:n, (n+1):p] is the same as 1:p . So what do you want to achieve?
Accepted Answer
Jan
on 9 Jun 2016
I guess boldly:
[s1, s2] = size(mon_innoilliq);
Result = zeros(s1, s2); % Pre-allocate
for i1 = 1:s1
for i2 = 1:s2
Result(i1, i2) = nanmean(mon_innoilliq(i1 , [1:i2-1, (i2+1):s2]), 2);
end
end
Does this create the wanted result?
0 Comments
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!