Clear Filters
Clear Filters

Average excluding values over 306x100 matrix

3 views (last 30 days)
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
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?
A. Goeh
A. Goeh on 9 Jun 2016
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

Sign in to comment.

Accepted Answer

Jan
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?

More Answers (0)

Community Treasure Hunt

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

Start Hunting!