how to replicate an array

前端 未结 4 1648
盖世英雄少女心
盖世英雄少女心 2020-11-30 16:08

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         


        
4条回答
  •  再見小時候
    2020-11-30 16:20

    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.

提交回复
热议问题