Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Reconstructing array

Subject: Reconstructing array

From: Young Ryu

Date: 22 Sep, 2010 20:25:07

Message: 1 of 5

Hi

I have an array:

A=rand(1200, 1200, 31);

I'd like to convert to [240, 240, 31] where 1200 by 1200 array is resized to 240 by 240 by using 5 by 5 window average. Is there any quick way rather than using for loop?

Thanks!

Subject: Reconstructing array

From: Sean

Date: 22 Sep, 2010 21:21:21

Message: 2 of 5

"Young Ryu" <ryuyr77@gmail.com> wrote in message <i7don3$ct7$1@fred.mathworks.com>...
> Hi
>
> I have an array:
>
> A=rand(1200, 1200, 31);
>
> I'd like to convert to [240, 240, 31] where 1200 by 1200 array is resized to 240 by 240 by using 5 by 5 window average. Is there any quick way rather than using for loop?
>
> Thanks!

If you have the Image Processing ToolBox and don't mind a short 1D for loop:

new = zeros(240,240,31);
for ii = 1:31
    new(:,:,ii) = blkproc(old(:,:,ii),[5 5],@(x)sum(x(:))/numel(x));
end
%Note sum(x(:))/numel(:) faster than mean(x(:))!

Subject: Reconstructing array

From: Sean

Date: 22 Sep, 2010 21:27:06

Message: 3 of 5

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i7ds0h$k69$1@fred.mathworks.com>...
> "Young Ryu" <ryuyr77@gmail.com> wrote in message <i7don3$ct7$1@fred.mathworks.com>...
> > Hi
> >
> > I have an array:
> >
> > A=rand(1200, 1200, 31);
> >
> > I'd like to convert to [240, 240, 31] where 1200 by 1200 array is resized to 240 by 240 by using 5 by 5 window average. Is there any quick way rather than using for loop?
> >
> > Thanks!
>
> If you have the Image Processing ToolBox and don't mind a short 1D for loop:
>
> new = zeros(240,240,31);
> for ii = 1:31
> new(:,:,ii) = blkproc(old(:,:,ii),[5 5],@(x)sum(x(:))/numel(x));
> end
> %Note sum(x(:))/numel(:) faster than mean(x(:))!

You could also do the one-liner (no IPT need):
new_mat = cellfun(@(x)sum(x(:))/numel(x),mat2cell(old,ones(1,240)*5,ones(1,240)*5,ones(1,31)));
Though it'll be slower than loops I'd imagine since cellfun/cell2mat is slow.

Subject: Reconstructing array

From: Matt J

Date: 22 Sep, 2010 21:34:04

Message: 4 of 5



Using the function below

A_resized=downsamp2d(A,[5,5]);


function M=downsamp2d(M,bindims)
%DOWNSAMP2D - simple tool for 2D binning slices of 3D array
%
% M=downsamp2d(M,bindims)
%
%in:
%
% M: a 3D array
% bindims: a vector [p,q] specifying pxq downsampling
%
%out:
%
% M: the downsized matrix

p=bindims(1); q=bindims(2);
[m,n,o]=size(M); %M is the original matrix

M=sum( reshape(M,p,[]) ,1 );
M=reshape(M,m/p,[]).'; %Note transpose

M=sum( reshape(M,q,[]) ,1);
M=reshape(M,n/q,[]).'; %Note transpose

M=M/(p*q);

M=reshape(M,m/p,m/q,o);

Subject: Reconstructing array

From: David Young

Date: 22 Sep, 2010 21:54:05

Message: 5 of 5

It might be worth do some timing tests to see how this approach compares:

 mask1 = ones([5 1 1])/5;
  B = convn(A, mask1, 'valid');
  mask2 = ones([1 5 1])/5;
  C = convn(B, mask2, 'valid');
  Result = C(1:5:end, 1:5:end, :);

You can combine the two convolutions into a single convolution, but that would make it slower. Even so, on the face of it this isn't specially efficient, because 96% of the computed values are thrown away in the last line - but because convolution is coded at a low level, the method may still prove competitive, especially compared to going via cell arrays. An experiment will easily show whether it is or not. No need for the IPT, by the way.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us