1

I want to make a function like this

>> matdup([1 2],3,4)            %or any other input that user wish to enter
ans= 

 1     2     1     2     1     2     1     2
 1     2     1     2     1     2     1     2
 1     2     1     2     1     2     1     2

I am stuck in my code. My logic:

m = matdup(input,row,col)
for i = 1:row
        for j = 1:col
            m(i, j)= input;

This is producing this:

>> matdup(1,2,2)
ans=

      1 1
      1 1

But failed at this:

>> matdup([1 2],3,4)

error at console:

Subscripted assignment dimension mismatch.

    Error in ==> matdup at 6
                m(i, j)= input

Any idea?

4 Answers 4

3

Method 1: Are you allowed to use ones? Try this -

A = [1 2]

rowIdx = [1 : size(A,1)]';
colIdx = [1 : size(A,2)]';

out = A(rowIdx(:, ones(3,1)), colIdx(:, ones(4,1)))

Output

out =

     1     2     1     2     1     2     1     2
     1     2     1     2     1     2     1     2
     1     2     1     2     1     2     1     2

Method 2: Are you allowed to use bsxfun and permute? Try this for the same result -

A = [1 2]
row_mapped = bsxfun(@plus,A,zeros(3,1))
out = reshape(bsxfun(@plus,row_mapped,permute(zeros(4,1),[3 2 1])),[3 8])
Sign up to request clarification or add additional context in comments.

1 Comment

@user3440716 Added one more method!
2

Matlab has a funcion called repmat that does the same.

If you want to create a similar function, you could do something like this:

function B = matdup(A, M, N)
    [nr, nc] = size(A);
    B = zeros([nr nc] .* [M N]);
    for r = 1:M
        for c = 1:N
            rr = (r - 1) * nr + 1;
            cc = (c - 1) * nc + 1;
            B(rr:rr + nr - 1, cc:cc + nc - 1) = A;
        end
    end
end

Note this function is restricted to 2D matrices.

1 Comment

i am not allowed to do so. any other thing. i ve idea but not convinced for example taking the size and implementing. but i cant think further what to do next. is it possible to look at library how they have made function?
2

Try kron:

matdup = @(x,m,n) kron(ones(m,n),x)

Demonstration:

>> A = [5 6 7];
>> out = matdup(A,3,2)
out =
     5     6     7     5     6     7
     5     6     7     5     6     7
     5     6     7     5     6     7

Note that you can switch the inputs to kron to effectively replicate elements rather than the whole matrix:

repel = @(x,m,n) kron(x,ones(m,n));

Demonstration:

>> A = [5 6 7];
>> out = repel(A,3,2)
out =
     5     5     6     6     7     7
     5     5     6     6     7     7
     5     5     6     6     7     7

Comments

2

The replication can be done easily using mod:

function R = matdup(A, M, N)
    [m n]= size(A);
    R = A(mod(0:m*M-1,m)+1, mod(0:n*N-1,n)+1)

1 Comment

Clever way to generate steps at a certain period. +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.