MATLAB: How to do a conditionnal mean

mean dataset

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.

Best Answer

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,'.-')