필터 지우기
필터 지우기

Computing the median of a group except one member

조회 수: 1 (최근 30일)
wy6622
wy6622 2019년 2월 13일
댓글: wy6622 2019년 2월 14일
I have a dataset of a variable x for S seasons of the same J firms (not ordered). I'd like to compute for each firm in each season, the median of all other firms in that season. e.g. S=2, J=6, my dataset is
M=[1 2 3 6 5 4 2 3 4 1 5 6; 1 1 1 1 1 1 2 2 2 2 2 2; 2 4 6 5 7 8 3 7 5 7 5 3]'
First column is the firm index, second column the season, third column the x values for each firm in each season.
I am thinking something like
for i=1:size(M,1)/6
for j=6*i-5:6*i
M(j,4)=median(M(6*i-5:6*i,3));
end
end
but with the index of M(:,3) inside median() somehow telling MATLAB to exclude the jth entry of M(:,3)?
Thank you!
  댓글 수: 8
madhan ravi
madhan ravi 2019년 2월 14일
ok did you get the answer to your question since you accepted the answer ?
wy6622
wy6622 2019년 2월 14일
It works for the most parts except the step where it tries to assign the computed median to the corresponding firm. The "row=..." row below returns 0's as opposed to the desired index.
M=[1 2 3 6 5 4 2 3 4 1 5 6; 1 1 1 1 1 1 2 2 2 2 2 2; 2 4 6 5 7 8 3 7 5 7 5 3]';
M(:,4)=0; %create column of zeros
num_seasons = unique(M(:,2)); %num of seasons
num_f = unique(M(:,1)); %num of firms
for i = 1:numel(num_seasons) % for each season...
season = M(M(:,2) == num_seasons(i),:); %matrix of season being iterated
for j = 1:numel(num_f) % for each firm...
med = median(season(not(season(:,1)==num_f(j)),3)); %median of OTHER firms for the season
disp(['Firm: ' num2str(num_f(j)) ' Season:' num2str(num_seasons(i)),...
' Median: ' num2str(med)]);
row = all(M(:,[1 2])==[num2str(num_f(j)) num2str(num_seasons(i))],2); %locate row where firm and season matches
M(row,4) = med; %append the median to the corresponding row, 4th col
end
end

댓글을 달려면 로그인하십시오.

채택된 답변

Kevin Phung
Kevin Phung 2019년 2월 14일
Hello!
Is this sort of what you're looking for?
M=[1 2 3 6 5 4 2 3 4 1 5 6; 1 1 1 1 1 1 2 2 2 2 2 2; 2 4 6 5 7 8 3 7 5 7 5 3]';
num_seasons = unique(M(:,2)); %num of seasons
num_f = unique(M(:,1)); %num of firms
for i = 1:numel(num_seasons) % for each season...
season = M(M(:,2) == num_seasons(i),:); %matrix of season being iterated
for j = 1:numel(num_f) % for each firm...
med = median(season(not(season(:,1)==num_f(j)),3)); %median of OTHER firms for the season
disp(['Firm: ' num2str(num_f(j)) ' Season:' num2str(num_seasons(i)),...
' Median: ' num2str(med)]);
end
end
  댓글 수: 6
Kevin Phung
Kevin Phung 2019년 2월 14일
Just for the sake of following up with my mistake:
I made the mistake of converting the values into strings. row should have been:
row = all(M(:,[1 2])==[num_f(j) num_seasons(i)],2); %locate row where firm and season matches
so this should have worked:
M=[1 2 3 6 5 4 2 3 4 1 5 6; 1 1 1 1 1 1 2 2 2 2 2 2; 2 4 6 5 7 8 3 7 5 7 5 3]';
M(:,4)=0; %create column of zeros
num_seasons = unique(M(:,2)); %num of seasons
num_f = unique(M(:,1)); %num of firms
for i = 1:numel(num_seasons) % for each season...
season = M(M(:,2) == num_seasons(i),:); %matrix of season being iterated
for j = 1:numel(num_f) % for each firm...
med = median(season(not(season(:,1)==num_f(j)),3)); %median of OTHER firms for the season
row = all(M(:,[1 2])==[num_f(j) num_seasons(i)],2); %locate row where firm and season matches
M(row,4) = med; %append the median to the corresponding row, 4th col
end
end
use whichever method works best for you
wy6622
wy6622 2019년 2월 14일
Thank you again for your help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by