For a project I'm asked to use rough interpolation on an image which is originally mxn values and convert it to an MxN matrix where each value from mxn gets duplicated nearby. So if my mxn matrix is:
A = [1 2 3;4 5 6]
A m = 2, n = 3 matrix and I want to interpolate by M = 2, N = 2 then my new matrix would be m*M x n*N (4x6 in this example):
A_int = [1 1 2 2 3 3; 1 1 2 2 3 3; 4 4 5 5 6 6; 4 4 5 5 6 6]
The code I'm using works fine but I'm wondering if there is an easier way to do this. Essentially it'll copy row 1 and repeat it M times downwards, then take row 2 and copy it M times downward, etc. Then take column 1 and copy it N times to the right, then column 2, etc.
function M_save = sample_interpolation(M, m, n)
[rows, cols, chs] = size(M); %determine the size of the incoming matrix
rows_new = rows * m; %calculate the size of the new matrix
cols_new = cols * n;
M_save = uint8(zeros(rows_new, cols_new, chs)); %create a new matrix full of zeros
a = 1:m:rows_new; %incremental value in step size of m
b = 1:n:cols_new; %incremental value in step size of n
M_save(a,b,:) = M(:,:,:); %save each incremented value as the value from the original matrix
for a = 1:m:rows_new %from the first row to the last skipping rows of zeros
for i = 1:m-1 %from the current row to the n-1 row upstream
M_save(i+a,:,:) = M_save(a,:,:); %save the value from a position in i+a
end
end
for a = 1:n:cols_new %from the first col to the last skipping rows of zeros
for i = 1:n-1 %from the current col to the m-1 row upstream
M_save(:,a+i,:) = M_save(:,a,:); %save the value from a position in i+a
end
end
figure('Name','Sample Effect')
imshow(M_save)
title([num2str(m),'x' ,num2str(n),' interpolation'])
end

 Réponse acceptée

Guillaume
Guillaume le 17 Avr 2018

1 vote

Assuming you're on R2015b or later, this is extremely easy:

A = [1 2 3;4 5 6];
M = 2;
N = 2;
A_int = repelem(A, M, N)

1 commentaire

Aron
Aron le 17 Avr 2018
Thank you so much. I knew there had to be a simpler way to do this and I appreciate your help. My for loops work, it's just convoluted.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by