How to use accumarray?
Show older comments
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
Accepted Answer
More Answers (1)
Matt J
on 30 Jun 2013
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 Comment
Ede gerlderlands
on 30 Jun 2013
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!