COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### resize array

• Follow

```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!
```
 0
Reply Young 12/4/2009 6:44:06 PM

```On Dec 4, 12:44=A0pm, "Young Ryu" <ryuy...@gmail.com> wrote:
> Hi
>
> I have an array:
> A=3Drand(1200, 1200);
>
> I'd like to resize this array by half size. I found the resizem function =
and it works:
>
> B=3Dresizem(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=3Drand(1200,1200);
B=3DA(1:2:end,1:2:end);
```
 0
Reply Frank 12/4/2009 7:06:53 PM

```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!
```
 0
Reply Lorenzo 12/4/2009 7:10:25 PM

```"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);
```
 0
Reply Matt 12/4/2009 7:28:09 PM

```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);
```
 0
Reply dbeirne (2) 6/14/2012 2:39:08 PM

```"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);
```
 0
Reply mattjacREMOVE (3177) 6/14/2012 3:29:07 PM

```"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);
```
 0
Reply mattjacREMOVE (3177) 6/14/2012 4:03:07 PM

6 Replies
437 Views

(page loaded in 0.073 seconds)

Similiar Articles:

7/22/2012 7:42:10 PM