how to do a conditionnal mean
이전 댓글 표시
Hi all,
I have many variables in a dataset.
One of them is called : FYEAR and another one is called : DY
I would like to find the mean of DY for each FYEAR and plot them.
Is there anyway i could do this ? can i do it with a boucle (IF for example ) ?
Thanks.
채택된 답변
추가 답변 (2개)
Jos (10584)
2014년 9월 23일
Something along these lines should work (assuming FYEAR and DY are numerical arrays)
FYEAR = [10 10 11 12 11 10 12 12 10]
DY = [ 1 3 4 8 6 2 10 9 2]
[UniqueFYEAR,~,i] = unique(FYEAR,'stable')
MeanDY = accumarray(k,DY,[],@mean)
plot(UniqueFYEAR, MeanDY, 'bo')
Here is yet another accumarray-based approach, but avoids a call to unique(), and also uses a faster 2-pass calculation,
FYEAR = [10 10 11 12 11 10 12 12 10];
DY = [ 1 3 4 8 6 2 10 9 2];
counts=accumarray(FYEAR(:),1)
sums=accumarray(FYEAR(:),DY(:));
idx=logical(counts);
means(idx)=sums(idx)./counts(idx);
카테고리
도움말 센터 및 File Exchange에서 Managing Data에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!