[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: Help with matrix replication
From: |
Richardson, Anthony |
Subject: |
RE: Help with matrix replication |
Date: |
Thu, 20 Dec 2012 17:13:53 +0000 |
> On Behalf Of Laurent Hoeltgen
> Subject: Re: Help with matrix replication
>
> On 20/12/12 17:50, Richardson, Anthony wrote:
> > I want to duplicate the rows in a matrix n times and then duplicate the
> columns n times.
> >
> > For example, if
> >
> > a =
> >
> > 1.0000e+00 2.0000e+00 3.0000e+00
> > 4.0000e+00 5.0000e+00 6.0000e+00
> >
> > for n= 2, I want
> >
> > b =
> >
> > 1.0000e+00 1.0000e+00 2.0000e+00 2.0000e+00 3.0000e+00
> 3.0000e+00
> > 1.0000e+00 1.0000e+00 2.0000e+00 2.0000e+00 3.0000e+00
> 3.0000e+00
> > 4.0000e+00 4.0000e+00 5.0000e+00 5.0000e+00 6.0000e+00
> 6.0000e+00
> > 4.0000e+00 4.0000e+00 5.0000e+00 5.0000e+00 6.0000e+00
> 6.0000e+00
> >
> > Note that this is not matrix replication(it is equivalent to using simple
> > pixel
> replication to zoom in on an image matrix). I thought I would be able to
> avoid
> loops by using some combination or repmat/repelems/reshape, but the best
> I could come up with was:
> >
> > b = reshape(repmat(reshape(repelems(a, [1:numel(a);
> > n(ones(1,numel(a)))])',n*rows(a),columns(a)),n,1),n*rows(a),n*columns(
> > a));
> >
> > After studying the "advanced indexing" section of the manual I came up
> with a more syntactically elegant (it does not appear to be any more time-
> efficient) solution:
> >
> > b = a((1:rows(a))(ones(1,n),:), (1:columns(a))(ones(1,n),:));
> >
> > I still feel as if I'm overlooking some simple method for doing what I want
> > to
> do. Does anyone have a better method?
> >
> > By the way, imresize(a, 2, 'nearest') will do what I want, but this is a
> function from the image package and I'm trying to write some routines that
> duplicate the functionality of the image package without using any of those
> functions.
> > Thanks
> > Tony Richardson
>
> Hi,
>
> try
>
> kron(a,[1 1 ; 1 1])
>
> for your case n=2. kron is the Kronecker Matrix product.
>
> Regards,
> Laurent
Thanks. This is certainly cleaner, although it seems to be a bit slower.
Tony