1

I'm trying to upsample a matrix by two by replicating another matrix, but i'm confused with the code, basically what i want is if:

Y = [1,2]

then the upsampled version would look like:

Up = [1,1,2,2;1,1,2,2]

What i've written so far is:

[row,col] = size(y)

Up = zeros(row*2,col*2);

for i = 1:2:row*2

     for j = 1:2:col*2

        Up(i, j) = Y(i,j);
        Up(i+1, j) = Y(i,j);
        Up(i, j+1) = Y(i,j);
        Up(i+1, j+1) = Y(i,j);

    end
end

but it says Index exceeds matrix dimensions, which i understand is because of the +1s but i'm not sure how else to go about doing this...

April
  • 35
  • 5
  • Does this work? `repmat(sort(repmat([1,2], 1, 2)), 2, 1)` – Alex Nov 10 '16 at 00:46
  • Are you doing this as a programming exercise, or do you just need to get the matrix resized? Because you could do this in one line with `kron` or `imresize`... – beaker Nov 10 '16 at 00:50
  • 1
    This question is relevant: http://stackoverflow.com/questions/16266804/matlab-repeat-every-column-sequentially-n-times – Alex Nov 10 '16 at 00:50
  • @Alex that first solution doesn't work because my data isn't in ascending order, that was just an example, but i'm looking through the second link right now – April Nov 10 '16 at 01:00
  • okay so my issue is that i need to replicated both column wise and row wise and i'm having trouble doing that at the same time without loops – April Nov 10 '16 at 01:02
  • 1
    Actually nevermind, removing the "sort" fixes the issue thank you! – April Nov 10 '16 at 01:05
  • removing the sort does not produce the desired output you have written in the question. – Alex Nov 10 '16 at 01:11
  • 1
    @AprilCrumb Try `kron(Y, ones(2))`. That should duplicate each element twice horizontally and vertically. – beaker Nov 10 '16 at 01:14
  • @Alex it does if you do it element by element, instead of replicating the whole matrix – April Nov 10 '16 at 03:30

2 Answers2

1

Data:

Y = [1,2]; % matrix
n = 2; % repetition factor

Solution using the repelem function (introduced in R2015a):

Up = repelem(Y,n,n);

Solution using indexing;

Up = Y(ceil(1/n:1/n:end), ceil(1/n:1/n:end));

Solution using a Kronecker product:

Up = kron(Y, ones(n))
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
0

The solution i ended up using is:

[row,col] = size(Y);
Up = zeros(row*2,col*2);
idx_row = 1;

for i = 1:D:row
    idx_col = 1;
    for j = 1:D:col

        Up(i:i+1, j:j+1) = repmat(repmat(Y(idx_row,idx_col),1,2),2,1);
       idx_col = idx_col + 1;
    end
     idx_row = idx_row + 1;
end
April
  • 35
  • 5