Thread Subject: How to expand a matrix?

Subject: How to expand a matrix?

From: Kian

Date: 2 May, 2009 22:02:02

Message: 1 of 21

Hi,

I have the following matrix:

[ 1 5
  4 9]

What's the fastest way, and without a for loop, to expand it to:

[ 1 1 5 5
  1 1 5 5
  4 4 9 9
  4 4 9 9]

or to

[ 1 1 1 5 5 5
  1 1 1 5 5 5
  1 1 1 5 5 5
  4 4 4 9 9 9
  4 4 4 9 9 9
  4 4 4 9 9 9]
?

Thanks in advance!

Subject: How to expand a matrix?

From: Bruno Luong

Date: 2 May, 2009 22:17:01

Message: 2 of 21

Help kron

A=[1 4; 4 9]

kron(A,ones(3))

% Bruno

Subject: How to expand a matrix?

From: Kian

Date: 2 May, 2009 22:23:02

Message: 3 of 21

Thanks Bruno. You always answer all of my questions.

Subject: How to expand a matrix?

From: Bruno Luong

Date: 2 May, 2009 22:55:03

Message: 4 of 21

You are welcome. If you want to force your processor to multiplication and addition unnecessary, and to avoid KRON then here is another way:

[m n]=size(A)
mdup=4; ndup=3;

AX=repmat(A,[1 1 mdup ndup]);
AX=permute(AX,[3 1 4 2]);
AX=reshape(AX,m*mdup,n*ndup)

% Bruno

Subject: How to expand a matrix?

From: Kian

Date: 2 May, 2009 23:07:01

Message: 5 of 21

I'm a bit confused. Why would I want to do it the second way? Is it faster? Is it more advantages for any reason? Seems like kron does it fast and easy.

Subject: How to expand a matrix?

From: Bruno Luong

Date: 2 May, 2009 23:21:01

Message: 6 of 21

I'm not sure. KRON is more general purpose, it performs many matrix multiplications. In our case it is trivial scalar with 1. Whereas the second method just duplicate and moving elements around. I'm not sure which one is faster.

For sure KRON is more readable.

Up to you

Bruno

Subject: How to expand a matrix?

From: Matt Fig

Date: 3 May, 2009 00:21:02

Message: 7 of 21

Just another method, using built-ins only:


% Data
A= [ 1 5;4 9]

% Engine
idx = cumsum(ones(2),2)
B = A(idx,idx)

Subject: How to expand a matrix?

From: Bruno Luong

Date: 3 May, 2009 06:56:01

Message: 8 of 21

Along the same line than Matt's solution using BSXOPS on FEX:

B = A(zeros(2,1)+(1:end),zeros(2,1)+(1:end))

% Bruno

Subject: How to expand a matrix?

From: Matt

Date: 3 May, 2009 13:24:01

Message: 9 of 21

Yet another method

[m n]=size(A)
mdup=4; ndup=3;

vxÎil((1:m*mdup)/mdup);
vyÎil((1:n*ndup)/ndup);

A(vx,vy)

Subject: How to expand a matrix?

From: Matt

Date: 3 May, 2009 13:29:02

Message: 10 of 21

Hmmm. Weird character translation problem...
 
 [m n]=size(A)
 mdup=4; ndup=3;
 
vxÎil((1:m*mdup)/mdup);
vyÎil((1:n*ndup)/ndup);

A(vx,vy)

Subject: How to expand a matrix?

From: Matt

Date: 3 May, 2009 15:45:02

Message: 11 of 21

"Matt " <xys@whatever.com> wrote in message <gtk66u$f3g$1@fred.mathworks.com>...
> Hmmm. Weird character translation problem...
>
> [m n]=size(A)
> mdup=4; ndup=3;
>
> vx?il((1:m*mdup)/mdup);
> vy?il((1:n*ndup)/ndup);
>
> A(vx,vy)

Well, the weird characters after vx and vy are supposed to be 'Îil'

vxÎil((1:m*mdup)/mdup);
vyÎil((1:n*ndup)/ndup);

Subject: How to expand a matrix?

From: Matt Fig

Date: 3 May, 2009 15:55:03

Message: 12 of 21

"Matt " <xys@whatever.com> wrote in message
> Well, the weird characters after vx and vy are supposed to be '?il'
>
> vx?il((1:m*mdup)/mdup);
> vy?il((1:n*ndup)/ndup);


Still not showing up, I will try. Do you mean ceil?

vx = ceil((1:m*mdup)/mdup);
vy = ceil((1:n*ndup)/ndup);

Subject: How to expand a matrix?

From: Matt

Date: 3 May, 2009 23:54:02

Message: 13 of 21

"Matt Fig" <spamanon@yahoo.com> wrote in message <gtkeon$rhl$1@fred.mathworks.com>...

> Still not showing up, I will try. Do you mean ceil?
>
> vx = ceil((1:m*mdup)/mdup);
> vy = ceil((1:n*ndup)/ndup);

Yes. for some reason '= ceil' without the space generates these weird characters...

Subject: How to expand a matrix?

From: Kian

Date: 5 May, 2009 19:08:01

Message: 14 of 21

"Matt Fig" <spamanon@yahoo.com> wrote in message <gtio1e$dj3$1@fred.mathworks.com>...
> Just another method, using built-ins only:
>
>
> % Data
> A= [ 1 5;4 9]
>
> % Engine
> idx = cumsum(ones(2),2)
> B = A(idx,idx)

I like this method. It's simple. But, how would you do this for a 3x3 matrix or a 4x4 matrx?

Subject: How to expand a matrix?

From: Matt

Date: 5 May, 2009 19:25:18

Message: 15 of 21

"Kian " <kian.torab@utah.edu> wrote in message <gtq2qh$blq$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message <gtio1e$dj3$1@fred.mathworks.com>...
> > Just another method, using built-ins only:
> >
> >
> > % Data
> > A= [ 1 5;4 9]
> >
> > % Engine
> > idx = cumsum(ones(2),2)
> > B = A(idx,idx)
>
> I like this method. It's simple. But, how would you do this for a 3x3 matrix or a 4x4 matrx?

My method is basically Matt Fig's idea, but generalized to arbitrary matrix size and arbitrary upsampling

 [m n]=size(A)
 mdup=4; ndup=3;
 
vx = ceil((1:m*mdup)/mdup);
vy = ceil((1:n*ndup)/ndup);

A(vx,vy)

Subject: How to expand a matrix?

From: Matt Fig

Date: 5 May, 2009 19:43:01

Message: 16 of 21

"Kian " <kian.torab@utah.edu> wrote in message <gtq2qh$blq$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message <gtio1e$dj3$1@fred.mathworks.com>...
> > Just another method, using built-ins only:
> >
> >
> > % Data
> > A= [ 1 5;4 9]
> >
> > % Engine
> > idx = cumsum(ones(2),2)
> > B = A(idx,idx)
>
> I like this method. It's simple. But, how would you do this for a 3x3 matrix or a 4x4 matrx?


Matt's method is more easily generalizable for arbitrary expansion in each direction. Nevertheless, here is a generalization of the method behind my example.

N = 3; % A is NxN
E = 4; % The expansion number.

A = round(rand(N)*10);
idx = cumsum(ones(E,N),2);
B = A(idx,idx)

Subject: How to expand a matrix?

From: us

Date: 5 May, 2009 20:17:01

Message: 17 of 21

"Matt Fig" <spamanon@yahoo.com> wrote in message <gtq4s5$6so$1@fred.mathworks.com>...
> "Kian " <kian.torab@utah.edu> wrote in message <gtq2qh$blq$1@fred.mathworks.com>...
> > "Matt Fig" <spamanon@yahoo.com> wrote in message <gtio1e$dj3$1@fred.mathworks.com>...
> > > Just another method, using built-ins only:
> > >
> > >
> > > % Data
> > > A= [ 1 5;4 9]
> > >
> > > % Engine
> > > idx = cumsum(ones(2),2)
> > > B = A(idx,idx)
> >
> > I like this method. It's simple. But, how would you do this for a 3x3 matrix or a 4x4 matrx?
>
>
> Matt's method is more easily generalizable for arbitrary expansion in each direction. Nevertheless, here is a generalization of the method behind my example.
>
> N = 3; % A is NxN
> E = 4; % The expansion number.
>
> A = round(rand(N)*10);
> idx = cumsum(ones(E,N),2);
> B = A(idx,idx)

matt - don't get sidetracked, don't procrastinate! you should be improving your FINDSUBMAT...

as ever with a :-)
urs

Subject: How to expand a matrix?

From: Matt Fig

Date: 5 May, 2009 20:27:01

Message: 18 of 21

"us " <us@neurol.unizh.ch> wrote in message
> matt - don't get sidetracked, don't procrastinate! you should be improving your FINDSUBMAT...
>
> as ever with a :-)
> urs


The watchful eye of Urs! ;-).

Subject: How to expand a matrix?

From: Bruno Luong

Date: 5 May, 2009 20:46:01

Message: 19 of 21

Re find submatrix: Matt, a lazy hint: it takes only two lines to typecast the double/single array to respectively int64/int32, then compares with strfind. The NaN issue will be resolved in no time. You might continue to hang around here. ;-)

Bruno

Subject: How to expand a matrix?

From: us

Date: 5 May, 2009 20:55:03

Message: 20 of 21

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gtq8i9$lpv$1@fred.mathworks.com>...
> Re find submatrix: Matt, ... The NaN issue will be resolved in no time. You might continue to hang around here. ;-)

matt: bruno - the devil in disguise!...

www.youtube.com/watch?v=m3_Q96eJr1k

[lol]
urs

Subject: How to expand a matrix?

From: Bruno Luong

Date: 5 May, 2009 21:04:02

Message: 21 of 21

:-)
Cheers,
Bruno

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
elvis presley us 5 May, 2009 17:14:25
expand matrix m... Kian 2 May, 2009 18:04:02
rssFeed for this Thread

Contact us at files@mathworks.com