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

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.

2 Comments

10 minutes sounds like you forgot the pre-allocation.
Thank you guys, that worked really well!!

Sign in to comment.

 Accepted Answer

A = [1, 2; ...
3, 4];
B = kron(A, ones(3));

3 Comments

Thanks so much for this answer! I was looking for a way to apply a correction factor after downsampling my data!
@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>
@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)

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

Asked:

on 4 Jun 2013

Commented:

on 23 Apr 2025

Community Treasure Hunt

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

Start Hunting!