How to resize a matrix repeating the original values N times...

14 views (last 30 days)
Hi all. Im trying to do some sort of UpSample on a matrix, what I need to do is repeat each value of my original matrix N times. Example with N=3
A =
1 2
3 4
A_up =
1 1 1 2 2 2
1 1 1 2 2 2
1 1 1 2 2 2
3 3 3 4 4 4
3 3 3 4 4 4
3 3 3 4 4 4
This is going to be used on images of 2Mpx and right now im using matrix concatenation to do it, but it takes more then 10min for each image, and I have to be able to do this in more then 800 images... If someone have a hint of how to do it faster I would be very gratefull.

Accepted Answer

Jan
Jan on 4 Jun 2013
A = [1, 2; ...
3, 4];
B = kron(A, ones(3));
  3 Comments
Voss
Voss on 23 Apr 2025
@Wesley: A more modern alternative is to use repelem (introduced in R2015a):
A = [1 2; 3 4]
A = 2×2
1 2 3 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
A_up = repelem(A,3,3)
A_up = 6×6
1 1 1 2 2 2 1 1 1 2 2 2 1 1 1 2 2 2 3 3 3 4 4 4 3 3 3 4 4 4 3 3 3 4 4 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Wesley
Wesley on 23 Apr 2025
@KONSTANTINOS VOSKAKISThat works even better for my application considering I just learned that the Kronecker product form matlab only works for 2D tensor rather than the 4D I work with!!!

Sign in to comment.

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 4 Jun 2013
Edited: Azzi Abdelmalek on 4 Jun 2013
N=3
out=cell2mat(arrayfun(@(x) ones(N)*x,A,'un',false))
Or
A=[1 2 3;4 5 6;7 8 9]
n=3
m=size(A,1)*n
out=reshape(repmat(reshape(repmat(A(:)',n,1),m,[]),n,1),m,[])

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!