Count instances of each unique integer in a vector in 1 line of code?

后端 未结 5 1014
你的背包
你的背包 2021-01-17 15:08

Is there a slick way to rewrite this Julia function, perhaps using just 1 line of code, without making it much slower? (I just started using Julia. It\'s great!) K

5条回答
  •  北恋
    北恋 (楼主)
    2021-01-17 15:32

    I haven't tested the performance, but using the hist function should work:

    hist(zd,0.5:K+0.5)[2]
    

    gives:

    5-element Array{Int64,1}: 1 4 1 0 0

    or, if the zeros are unimportant, just use

    hist(zd)[2]
    3-element Array{Int64,1}:
     1
     4
     1
    

提交回复
热议问题