Thread Subject:
resize array

Subject: resize array

From: Young Ryu

Date: 4 Dec, 2009 18:44:06

Message: 1 of 7

Hi

I have an array:
A=rand(1200, 1200);

I'd like to resize this array by half size. I found the resizem function and it works:

B=resizem(A, 0.5);

However, I felt this function is quite slow. I guess that there might be a faster function to do this. Could you help me?

Thanks!

Subject: resize array

From: Frank

Date: 4 Dec, 2009 19:06:53

Message: 2 of 7

On Dec 4, 12:44 pm, "Young Ryu" <ryuy...@gmail.com> wrote:
> Hi
>
> I have an array:
> A=rand(1200, 1200);
>
> I'd like to resize this array by half size. I found the resizem function and it works:
>
> B=resizem(A, 0.5);
>
> However, I felt this function is quite slow. I guess that there might be a faster function to do this. Could you help me?
>
> Thanks!

If you want ever other value, for example, you can use:

A=rand(1200,1200);
B=A(1:2:end,1:2:end);

Subject: resize array

From: Lorenzo Guerrasio

Date: 4 Dec, 2009 19:10:25

Message: 3 of 7

I don't have the resizem function.This code does what resize(A,.5) does, don't know if faster...

A=rand(1200);
s=size(A);
i1=(1:ceil(s(1)/2)).*2;
i2=(1:ceil(s(2)/2)).*2;
B=A(i1,i2);

"Young Ryu" <ryuyr77@gmail.com> wrote in message <hfbl9m$pv1$1@fred.mathworks.com>...
> Hi
>
> I have an array:
> A=rand(1200, 1200);
>
> I'd like to resize this array by half size. I found the resizem function and it works:
>
> B=resizem(A, 0.5);
>
> However, I felt this function is quite slow. I guess that there might be a faster function to do this. Could you help me?
>
> Thanks!

Subject: resize array

From: Matt J

Date: 4 Dec, 2009 19:28:09

Message: 4 of 7

"Young Ryu" <ryuyr77@gmail.com> wrote in message <hfbl9m$pv1$1@fred.mathworks.com>...

Here's a function for downsampling, but it works only when the new pixel sizes are integer multiples of the old ones. If this is enough for you, it might be faster than resizem, if the latter is trying to do something interpolation-based.

function M=downsamp2d(M,bindims)
%DOWNSAMP2D - 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

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);

Subject: resize array

From: Deanna

Date: 14 Jun, 2012 14:39:08

Message: 5 of 7

Works like a champ. You the man!!

"Matt J" wrote in message <hfbns9$90v$1@fred.mathworks.com>...
> "Young Ryu" <ryuyr77@gmail.com> wrote in message <hfbl9m$pv1$1@fred.mathworks.com>...
>
> Here's a function for downsampling, but it works only when the new pixel sizes are integer multiples of the old ones. If this is enough for you, it might be faster than resizem, if the latter is trying to do something interpolation-based.
>
> function M=downsamp2d(M,bindims)
> %DOWNSAMP2D - 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
>
> 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);

Subject: resize array

From: Matt J

Date: 14 Jun, 2012 15:29:07

Message: 6 of 7

"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);

Subject: resize array

From: Matt J

Date: 14 Jun, 2012 16:03:07

Message: 7 of 7

"Matt J" wrote in message <jrd003$3kb$1@newscl01ah.mathworks.com>...
>
> M=sum(sum(M,1),3)/(p*q);

Better still, make this

M=mean(mean(M,1),3);

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
rebinning Matt J 14 Jun, 2012 11:29:09
rssFeed for this Thread

Contact us