How do I initialise all entries of a matrix with a specific value?

后端 未结 5 1313
南笙
南笙 2020-12-14 06:04

In Haskell, if I wanted to get a 10 element list which only contained the number 5, I could do something like this:

take 10 $ repeat 5

Outp

5条回答
  •  不思量自难忘°
    2020-12-14 06:37

    The ones method is much faster than using repmat:

    >> tic; for i = 1:1e6, x=5*ones(10,1); end; toc
    Elapsed time is 3.426347 seconds.
    >> tic; for i = 1:1e6, y=repmat(5,10,1); end; toc
    Elapsed time is 20.603680 seconds. 
    

    And, in my opinion, makes for much more readable code.

提交回复
热议问题