As far as I'm aware, there are two nice ways to tell the comprehension the 
type that is going to be returned.

Option 1: Tell it the type of the comprehension:

K = Float64[dot(X[:,i], X[:,j]) for i=1:5, j=1:5]


Option 2: Assert the type of the return of "dot"

K = [dot(X[:,i],X[:,j])::Float64 for i=1:5, j=1:5]

As far as I can tell, they'll do the same thing.

On Tuesday, 10 November 2015 02:37:17 UTC+10, rizal zaini wrote:
>
> Hi all
>
> I want to create matrix containing dot products
>
> julia> X = rand(5,5)
> 5x5 Array{Float64,2}:
>  0.274799   0.564619  0.915595  0.341059  0.57361
>  0.0806822  0.293939  0.379279  0.608582  0.652441
>  0.264678   0.532751  0.289088  0.457759  0.492777
>  0.33856    0.495587  0.499099  0.548237  0.402534
>  0.686305   0.168385  0.8322    0.605532  0.518693
>
> julia>  K = [dot(X[:,i], X[:,j]) for i=1:5, j=1:5]
> 5x5 Array{Any,2}:
>  0.737716  0.603229  1.09884  0.865174  0.832958
>  0.603229  0.962978  1.16994  0.988988  1.06501
>  1.09884   1.16994   2.00739  1.45298   1.54767
>  0.865174  0.988988  1.45298  1.36347   1.35304
>  0.832958  1.06501   1.54767  1.35304   1.42861
>
> I expect to get an Array{Float64,2}, but what I got is Array{Any,2}. 
> How to get Array{Float64,2} using Array comprehension?
>
> Thanks before
> Rizal
>

Reply via email to