How to use accumarray?

5 ビュー (過去 30 日間)
Ede gerlderlands
Ede gerlderlands 2013 年 6 月 30 日
I have a matrix of y=[9, 5346]
I want to find the the mean of every six values of each row. I tried this function but can't really figure out how to do it
for ii=1:9
oo(ii)=accumarray(1:6:5346,y(ii,:),,[],@mean);
end
it's saying there is error in this code . I don't really know how this accumarray works.
Any help is appreciated

採用された回答

the cyclist
the cyclist 2013 年 6 月 30 日
編集済み: the cyclist 2013 年 6 月 30 日
accumarray() can be a bit tricky to learn. I think this solves your problem. I commented it to help you see what is happening.
A = rand(9,5346); % Make up some fake data that is the same size as yours
At = A'; % Take the transpose, because accumarray works down columns.
numberPerGroup = 6;
numberGroups = 5346/6; % 931, but wanted you to see where this comes from
% The subs array indicates which rows (of the transposed array) are going to get grouped. You have 931 groups total, each gather 6 rows.
subs = repmat(1:numberGroups,[numberPerGroup,1]);
subs = subs(:);
% Preallocate the output array
output = zeros(numberGroups,9);
% Accumulate each column in turn
for nr = 1:9
output(:,nr) = accumarray(subs,At(:,nr),[],@mean);
end
% Transpose the output back
output = output';
  1 件のコメント
Ede gerlderlands
Ede gerlderlands 2013 年 6 月 30 日
Thank you ..

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

その他の回答 (1 件)

Matt J
Matt J 2013 年 6 月 30 日
Although you can do this with accumarray, it will probably be faster to do
oo=downsampn(y,[1,6]);
using the function below.
function M=downsampn(M,bindims)
%DOWNSAMPN - simple tool for downsampling n-dimensional nonsparse arrays
%
% M=downsampn(M,bindims)
%
%in:
%
% M: an array
% bindims: a vector of integer binning dimensions
%
%out:
%
% M: the downsized array
nn=length(bindims);
[sz{1:nn}]=size(M); %M is the original array
sz=[sz{:}];
newdims=sz./bindims;
args=num2cell([bindims;newdims]);
M=reshape(M,args{:});
for ii=1:nn
M=mean(M,2*ii-1);
end
M=reshape(M,newdims);
  1 件のコメント
Ede gerlderlands
Ede gerlderlands 2013 年 6 月 30 日
Yes, thank you.

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by