|
"Deanna" wrote in message <jrct2c$jbi$1@newscl01ah.mathworks.com>...
>
>
> Works like a champ. You the man!!
==================
That's kind of you, Deanna, but since that post several years ago the Newsgroup has discussed still more efficient ways to do this. The function below (bin2d) is even faster and more memory efficient (using a method proposed by Bruno Luong, if I recall correctly). Both downsamp2d and bin2d are only appropriate for non-sparse arrays, incidentally.
M=rand(5000);
bindims=[2,2];
tic;
m1=downsamp2d(M,bindims);
toc;
%Elapsed time is 0.311892 seconds.
tic;
m2=bin2d(M,bindims);
toc;
%Elapsed time is 0.184764 seconds.
function M=bin2d(M,bindims)
%BIN2D - simple tool for 2D downsampling
%
% M=downsamp2d(M,bindims)
%
%in:
%
% M: a matrix
% bindims: a vector [p,q] specifying pxq downsampling
%
%out:
%
% M: the downsized matrix
p=bindims(1);
q=bindims(2);
[m,n]=size(M); %M is the original matrix
newdims=[m/p,n/q];
M=reshape(M,p,newdims(1),q,newdims(2));
M=sum(sum(M,1),3)/(p*q);
M=reshape(M,newdims);
|