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
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?
A. Goeh
A. Goeh 2016-6-9
Hello, I just noticed that it wasn't very clear what I was trying to achieve. Also that, like you say, my inner loop wasn't connected at all. I rearranged my thoughts and came up with a solution myself. Here is what I tried to explain:
% Averages over all firms without the company itself
[a,p]=size(mon_innoilliq); % get size of the data-sheet
for m=1:a % loop through every row
for n=1:p; %loop through every column
marktdurchilliq(m, n) = nanmean( mon_innoilliq(m, 1:p ~= n));
end
end
The biggest problem was coming up that I had to leave out the indexed variable itself. Thank you very much for your quick reply!
Greets, A. Goeh

请先登录,再进行评论。

采纳的回答

Jan
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 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by