MATLAB: Average excluding values over 306×100 matrix

averageindexingiteratemean

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.

Best Answer

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?
Related Question