repmat vs simple matrix multiplication in MATLAB

前端 未结 2 1876
陌清茗
陌清茗 2021-01-25 02:00

Let v be a row vector of length n. The goal is to create a matrix A with m rows that are all equal to v.

MATLAB has a function for this that is

2条回答
  •  灰色年华
    2021-01-25 02:04

    Lets compare them!

    When testing algorithms 2 metrics are important: time, and memory.

    Lets start with time:

    Clearly repmat wins!

    Memory:

    profile -memory on
    for m=1000:1000:50000
    f1=@()(repmat(v,[m 1]));
    f2=@()(ones(m,1)*v);
    ii=ii+1;
    t1(ii)=timeit(f1);
    t2(ii)=timeit(f2);
    end
    profreport
    

    It seems that both take the same amount of memory. However, the profiler is known for not showing all the memory, so we can not fully trust it.

    Still, it is clear that repmat is better

提交回复
热议问题