Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Data Bin
Date: Tue, 6 Oct 2009 09:05:06 +0000 (UTC)
Organization: Erasmus MC
Lines: 32
Message-ID: <haf182$dvq$1@fred.mathworks.com>
References: <haafq5$mvs$1@fred.mathworks.com> <had81v$f9l$1@fred.mathworks.com> <hadjaf$pn1$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1254819906 14330 172.30.248.38 (6 Oct 2009 09:05:06 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 6 Oct 2009 09:05:06 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 870065
Xref: news.mathworks.com comp.soft-sys.matlab:575227


"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hadjaf$pn1$1@fred.mathworks.com>...
> Dear AP Pal!
> 
> > Any fast way to reshape the whole matrix?
> > i mean converting from 100X 50
> > to                               10X  50
> > 
> > by averaging the 10  rows at a time to bin
> > 
> > the reshape funtion worked for n X 1 matrix only.
> 
> No, RESHAPE works really fast for any array, not just for [n x 1] matrices!
> 
> The mentioned solution is efficient. A little bit faster, because it avoids the overhead of calling the function MEAN:
>   X = rand(100, 50);
>   Y = sum(reshape(X, 10, 10, 50), 1) / 10;
> 
> Kind regards, Jan

If the number of elements is not a multiple of the number you want to average over, you can use ACCUMARRAY

A = 1:8
n = 3 ;

% M = sum(reshape(A,n,[]))/10 % fails !!
idx = 1:numel(A) 
groupnr = ceil(idx / n) 
M = accumarray(groupnr(:),A(:),[],@mean)
% or in 1 step, 
% M = accumarray(ceil((1:numel(A))/n).',A.',[],@mean)

Jos