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 件のコメント

Matt J
Matt J 2014 年 9 月 23 日
can i do it with a boucle
This kind of boucle?
Guillaume
Guillaume 2014 年 9 月 23 日
'boucle' is french for 'loop'

サインインしてコメントする。

 採用された回答

the cyclist
the cyclist 2014 年 9 月 23 日

0 投票

You can use accumarray() to do this:
FYEAR = [2001; 2001; 2002; 2002];
DY = [1; 2; 5; 6];
[uniqueYear,i,j] = unique(FYEAR);
meanDY = accumarray(j,DY,[],@mean)
uniqueYear is the list of years in your list, and meanDY is the mean values.
There are many way to plot them. Here is a simple one:
plot(uniqueYear,meanDY,'.-')

1 件のコメント

alex monjambo
alex monjambo 2014 年 9 月 23 日
Thanks to all :) !

サインインしてコメントする。

その他の回答 (2 件)

Jos (10584)
Jos (10584) 2014 年 9 月 23 日

0 投票

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')
Matt J
Matt J 2014 年 9 月 23 日
編集済み: Matt J 2014 年 9 月 23 日

0 投票

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);

カテゴリ

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by