repmat vs simple matrix multiplication in MATLAB

前端 未结 2 1877
陌清茗
陌清茗 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:13

    You should use repmat().

    Matrix Multiplication is O(n ^ 3) operation which is much slower then replicating data in memory.
    On top of that, the second option allocate more data in memory of the size of the output.

    In the case above you create a vector which the outer multiplication is faster yet still not as memory operation.

    MATLAB doesn't use the knowledge all vector elements are 1, hence you multiply each element of x by 1 m times.

    Both operations will be mainly memory bounded, yet more efficient, fast and direct method would be going with repmat().

    The question is, what you do afterwards?
    Because you may not need repmat().

提交回复
热议问题