Average excluding values over 306x100 matrix
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
Jan
2016-6-9
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?
采纳的回答
Jan
2016-6-9
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!