pp11
pp11

Reputation: 309

syntax confusion: function call versus array indexing

Original title: "Kronecker product in Julia"

Assume:

 p       = 0.7;
 PI      = [p 1-p;1-p p];

and:

 Q       = zeros(20,20);

In Matlab we can run:

A=kron(PI(j,:),Q)

while in Julia:

A=kron[PI[j,:],Q]

this leads to the following error:

MethodError: no method matching getindex(::Base.#kron, ::Array{Float64,1}, ::Array{Float64,2})

How to address this and get a result similar to Matlab?

Upvotes: 1

Views: 133

Answers (1)

DSM
DSM

Reputation: 353339

There are two uses of () in your line in Matlab:

A=kron(PI(j,:),Q)

The outer () surround the arguments being passed to the kron function, and the inner () provide the index into PI. In Julia (and Python, and C, and many languages) we use different symbols for these two distinct purposes.

In Julia, we use square brackets [ and ] for indexing, and ( and ) to surround function arguments.

So:

julia> kron(PI[1, :], Q)
40×20 Array{Float64,2}:
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  …  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0     0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0     0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0     0.0  0.0  0.0  0.0  0.0  0.0  0.0
 [etc.]

Upvotes: 4

Related Questions