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:
Matrix resize with interpolation, please help!

Subject: Matrix resize with interpolation, please help!

From: Tyler Durdon

Date: 4 Dec, 2007 02:17:09

Message: 1 of 12

I'm trying to expand a 3d matrix in 1 dimension. For
example if I start with [5,5,5] matrix, I want to expand it
to [5,5,12]. In that case I expand to 12, but I want this
number to be variable and interpolate the missing cells. I
can do this using the following, where k is the expansion
constant, and M is the original matrix.

d = size(M);
[xi,yi,zi] = meshgrid(1:d(1),1:d(2),1:1/k:d(3));
M_resized = interp3(M,xi,yi,zi,interp_method);

but meshgrid takes up too much memory for very large matrix.
 Any ideas would be greatly appreciated, Thanks in advance.

Subject: Matrix resize with interpolation, please help!

From: Bruno Luong

Date: 4 Dec, 2007 07:45:03

Message: 2 of 12

"Tyler Durdon" <sparkchaser2005@yahoo.com> wrote in message
<fj2db5$83e$1@fred.mathworks.com>...
>
> but meshgrid takes up too much memory for very large matrix.

It's like asking how you transport 50 people in a passenger
car. Solution: do it with a bus.

Bruno

Subject: Matrix resize with interpolation, please help!

From: Bruno Luong

Date: 4 Dec, 2007 08:36:51

Message: 3 of 12

"Tyler Durdon" <sparkchaser2005@yahoo.com> wrote in message
<fj2db5$83e$1@fred.mathworks.com>...

>
> d = size(M);
> [xi,yi,zi] = meshgrid(1:d(1),1:d(2),1:1/k:d(3));
> M_resized = interp3(M,xi,yi,zi,interp_method);

BTW, you do not need to call interp3, just interp1 is OK.

d=size(M);
z=1:d(3);
zi=1:1/k:d(3);
M=permute(interp1(z,permute(M,[3 1 2]),zi),[2 3 1]);

You still get one big 3D array though.

Bruno

Subject: Matrix resize with interpolation, please help!

From: Tyler Durdon

Date: 4 Dec, 2007 18:00:33

Message: 4 of 12

"Bruno Luong" <brunoluong@yahoo.com> wrote in message
<fj33j3$lt6$1@fred.mathworks.com>...
> "Tyler Durdon" <sparkchaser2005@yahoo.com> wrote in message
> <fj2db5$83e$1@fred.mathworks.com>...
>
> >
> > d = size(M);
> > [xi,yi,zi] = meshgrid(1:d(1),1:d(2),1:1/k:d(3));
> > M_resized = interp3(M,xi,yi,zi,interp_method);
>
> BTW, you do not need to call interp3, just interp1 is OK.
>
> d=size(M);
> z=1:d(3);
> zi=1:1/k:d(3);
> M=permute(interp1(z,permute(M,[3 1 2]),zi),[2 3 1]);
>
> You still get one big 3D array though.
>
> Bruno

Hi Bruno,
  Thanks a lot for this code, you have saved me a lot of
time. It has solved the memory problems I was having and it
is faster.

Subject: Matrix resize with interpolation, please help!

From: Tyler Durdon

Date: 5 Dec, 2007 02:07:24

Message: 5 of 12

"Bruno Luong" <brunoluong@yahoo.com> wrote in message
<fj33j3$lt6$1@fred.mathworks.com>...
> "Tyler Durdon" <sparkchaser2005@yahoo.com> wrote in message
> <fj2db5$83e$1@fred.mathworks.com>...
>
> >
> > d = size(M);
> > [xi,yi,zi] = meshgrid(1:d(1),1:d(2),1:1/k:d(3));
> > M_resized = interp3(M,xi,yi,zi,interp_method);
>
> BTW, you do not need to call interp3, just interp1 is OK.
>
> d=size(M);
> z=1:d(3);
> zi=1:1/k:d(3);
> M=permute(interp1(z,permute(M,[3 1 2]),zi),[2 3 1]);
>
> You still get one big 3D array though.
>
> Bruno

One more question, interp1 only accepts 'single' precision
values. Is there a way to do this with 'uint8' values, this
would save even more space because my matrix cells vary only
between 0 and 255. Any help is greatly appreciated.

Subject: Matrix resize with interpolation, please help!

From: Jennifer

Date: 16 Aug, 2011 15:09:26

Message: 6 of 12

> > d=size(M);
> > z=1:d(3);
> > zi=1:1/k:d(3);
> > M=permute(interp1(z,permute(M,[3 1 2]),zi),[2 3 1]);
> >

I want to do something similar - resizing a 2D matrix by effectively inserting columns and rows and then interpolating to populate the the inserted values, e.g. a 121x121 matrix needs to be resized to 161x161 by inserting new columns and rows after every existing third column and row.

Can anyone help?

Subject: Matrix resize with interpolation, please help!

From: Matt J

Date: 16 Aug, 2011 17:53:31

Message: 7 of 12

"Jennifer" wrote in message <j2e176$5vk$1@newscl01ah.mathworks.com>...
>
>
> I want to do something similar - resizing a 2D matrix by effectively inserting columns and rows and then interpolating to populate the the inserted values, e.g. a 121x121 matrix needs to be resized to 161x161 by inserting new columns and rows after every existing third column and row.
=================

One way:


z1=1:121;
z2=3.5:3:121;
z=sort([z1,z2]);

YourMatrix=rand(161);

tmp=interp1(YourMatrix,z);
result=interp1(tmp.',z);

Subject: Matrix resize with interpolation, please help!

From: Jennifer

Date: 17 Aug, 2011 14:30:14

Message: 8 of 12

Thanks Matt, but I found a way to do it as follows:

% inputMatrix is the original matrix to be resized and then interpolated
% in this case inputMatrix will be converted from 121x121 to 161x161
maxVal = size(inputMatrix,1);
[X,Y] = meshgrid(1:1:maxVal);
%dInterval is the grid size for the interpolated matrix
dInterval = 0.75;
[XI,YI] = meshgrid(1:dInterval:maxVal);
Z = inputMatrix;
reSized = interp2(X,Y,Z,XI,YI);

Subject: Matrix resize with interpolation, please help!

From: ImageAnalyst

Date: 17 Aug, 2011 15:33:38

Message: 9 of 12

On Aug 17, 10:30 am, "Jennifer " <jenb...@srlab.co.uk> wrote:
> Thanks Matt, but I found a way to do it as follows:
>
> % inputMatrix is the original matrix to be resized and then interpolated
> % in this case inputMatrix will be converted from 121x121 to 161x161
> maxVal = size(inputMatrix,1);
> [X,Y] = meshgrid(1:1:maxVal);
> %dInterval is the grid size for the interpolated matrix
> dInterval = 0.75;
> [XI,YI] = meshgrid(1:dInterval:maxVal);
> Z = inputMatrix;
> reSized = interp2(X,Y,Z,XI,YI);

---------------------------
Is there some reason why you're not using imresize()?

Subject: Matrix resize with interpolation, please help!

From: Matt J

Date: 17 Aug, 2011 18:04:09

Message: 10 of 12

"Jennifer" wrote in message <j2gj9m$h7d$1@newscl01ah.mathworks.com>...
> Thanks Matt, but I found a way to do it as follows:
>
> % inputMatrix is the original matrix to be resized and then interpolated
> % in this case inputMatrix will be converted from 121x121 to 161x161
> maxVal = size(inputMatrix,1);
> [X,Y] = meshgrid(1:1:maxVal);
> %dInterval is the grid size for the interpolated matrix
> dInterval = 0.75;
> [XI,YI] = meshgrid(1:dInterval:maxVal);
> Z = inputMatrix;
> reSized = interp2(X,Y,Z,XI,YI);
=====================

This is a little different than what you initially said you were trying to do. In any case, it's a relatively slow and memory-expensize approach. Below is a comparison with several more methods. IMRESIZE as suggested by ImageAnalyst is the clear winner, but you would need the Image Processing Toolbox for that.



inputMatrix=rand(1210);
dInterval = 0.75;
maxVal = size(inputMatrix,1);


tic;
[X,Y] = meshgrid(1:1:maxVal);
%dInterval is the grid size for the interpolated matrix
[XI,YI] = meshgrid(1:dInterval:maxVal);
Z = inputMatrix;
reSized1 = interp2(X,Y,Z,XI,YI);
toc;
%Elapsed time is 0.749998 seconds.



%Method 2 - decompose into 2 INTERP1 operations
tic;
 z=1:dInterval:maxVal;
 reSized2=interp1(inputMatrix,z);
 reSized2=interp1(reSized2.',z).';
toc
%Elapsed time is 0.419842 seconds.




%Method 3 - using sparse matrices
tic;
 M=eye(maxVal);
  z=1:dInterval:maxVal;
 M=sparse(interp1(M,z));
 reSized4=M*inputMatrix*M.';
toc
%Elapsed time is 0.221216 seconds.


%Method 4 - using RESIZE (but you need IPT for this)
tic;
reSized3=imresize(inputMatrix,1/dInterval);
toc
%Elapsed time is 0.043478 seconds.

Subject: Matrix resize with interpolation, please help!

From: Bruno Luong

Date: 17 Aug, 2011 18:36:09

Message: 11 of 12

"Matt J" wrote in message <j2gvqp$12o$1@newscl01ah.mathworks.com>...
> "Jennifer" wrote in message <j2gj9m$h7d$1@newscl01ah.mathworks.com>...

>
> %Method 3 - using sparse matrices
> tic;
> M=eye(maxVal);
> z=1:dInterval:maxVal;
> M=sparse(interp1(M,z));
> reSized4=M*inputMatrix*M.';
> toc
> %Elapsed time is 0.221216 seconds.
>

Good idea, however much of the time is wasted because inefficient building of M. This is faster:

tic
xi = 1:dInterval:maxVal;
j1 = floor(xi);
j2 = j1+1;
w1 = j2-xi;
w2 = 1-w1;
j2(end) = maxVal;
m = length(xi);
n = maxVal;
S = sparse([1:m 1:m], [j1 j2], [w1 w2], m, n);
reSized5 = S*inputMatrix*S.';
toc % 0.027987 seconds.

Bruno

Subject: Matrix resize with interpolation, please help!

From: Matt J

Date: 17 Aug, 2011 19:07:09

Message: 12 of 12

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j2h1mp$7mq$1@newscl01ah.mathworks.com>...
>
>
> Good idea, however much of the time is wasted because inefficient building of M. This is faster:
>

Very nice. And that beats IMRESIZE (on my machine, at least).

Tags for 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