|
From: | Richardson, Anthony |
Subject: | Help with matrix replication |
Date: | Thu, 20 Dec 2012 16:50:31 +0000 |
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 |
[Prev in Thread] | Current Thread | [Next in Thread] |