1

accumarray各グループの最後以外のすべての観測を削除する方法はありますか?

私が念頭に置いていたのは、似たようなものでした:

lastobs=accumarray(bin,x,[],@(x){pick the observation with the max index in each group};  

例を挙げると、次のようなものがあるとします。

bin=[1 2 3  3 3 4 4];  %#The bin where the observations should be put
x= [21 3 12 5 6 8 31]; %#The vector of observations
%#The output I would like is as follow  
lastobs=[21 3 6 31];  

私が実際に考えているのはaccumarray、それを使用して各ビンの観測値の平均を計算したからです。したがって、トリックを作成できるすべての機能は、私にとっては問題ありません。

4

2 に答える 2

4

もちろん、あなたはこれを行うことができますaccumarrayx(end)配列内の最後の観測です。これが機能するにはソートする必要があることに注意してくださいbin。ソートされていない場合は、 [bin,sortIdx]=sort(bin);x = x(sortIdx);最初に実行してください。

lastobs = accumarray(bin(:),x(:),[],@(x)x(end)); %# bin, x, should be n-by-1
于 2014-02-24T15:59:09.517 に答える