Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to expand a matrix?

2 views
Skip to first unread message

Kian

unread,
May 2, 2009, 6:02:02 PM5/2/09
to
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!

Bruno Luong

unread,
May 2, 2009, 6:17:01 PM5/2/09
to
Help kron

A=[1 4; 4 9]

kron(A,ones(3))

% Bruno

Kian

unread,
May 2, 2009, 6:23:02 PM5/2/09
to
Thanks Bruno. You always answer all of my questions.

Bruno Luong

unread,
May 2, 2009, 6:55:03 PM5/2/09
to
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

Kian

unread,
May 2, 2009, 7:07:01 PM5/2/09
to
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.

Bruno Luong

unread,
May 2, 2009, 7:21:01 PM5/2/09
to
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

Matt Fig

unread,
May 2, 2009, 8:21:02 PM5/2/09
to
Just another method, using built-ins only:


% Data
A= [ 1 5;4 9]

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

Bruno Luong

unread,
May 3, 2009, 2:56:01 AM5/3/09
to
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

Matt

unread,
May 3, 2009, 9:24:01 AM5/3/09
to
Yet another method

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

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

A(vx,vy)

Matt

unread,
May 3, 2009, 9:29:02 AM5/3/09
to
Hmmm. Weird character translation problem...

Matt

unread,
May 3, 2009, 11:45:02 AM5/3/09
to
"Matt " <x...@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 '=ceil'

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

Matt Fig

unread,
May 3, 2009, 11:55:03 AM5/3/09
to
"Matt " <x...@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);

Matt

unread,
May 3, 2009, 7:54:02 PM5/3/09
to
"Matt Fig" <spam...@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...

Kian

unread,
May 5, 2009, 3:08:01 PM5/5/09
to
"Matt Fig" <spam...@yahoo.com> wrote in message <gtio1e$dj3$1...@fred.mathworks.com>...

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

Matt

unread,
May 5, 2009, 3:25:18 PM5/5/09
to
"Kian " <kian....@utah.edu> wrote in message <gtq2qh$blq$1...@fred.mathworks.com>...

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)

Matt Fig

unread,
May 5, 2009, 3:43:01 PM5/5/09
to
"Kian " <kian....@utah.edu> wrote in message <gtq2qh$blq$1...@fred.mathworks.com>...


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)

us

unread,
May 5, 2009, 4:17:01 PM5/5/09
to
"Matt Fig" <spam...@yahoo.com> wrote in message <gtq4s5$6so$1...@fred.mathworks.com>...

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

as ever with a :-)
urs

Matt Fig

unread,
May 5, 2009, 4:27:01 PM5/5/09
to
"us " <u...@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! ;-).

Bruno Luong

unread,
May 5, 2009, 4:46:01 PM5/5/09
to
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

us

unread,
May 5, 2009, 4:55:03 PM5/5/09
to
"Bruno Luong" <b.l...@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

Bruno Luong

unread,
May 5, 2009, 5:04:02 PM5/5/09
to
:-)
Cheers,
Bruno
0 new messages