Gabriel Machado
Gabriel Machado

Reputation: 493

Array{Float64,2} problem when multiplying matrixes Julia

I was doing something fairly simple with matrixes:

for i in 1:N
  X=XT[1:2,i]
  A[i]=X'*Sig*X+b
end

Where XT is an Array of arrays (1×5 Array{Any,2}) that I imported from Matlab, and by doing so I get X of type Array{Float64,2}. And Sig is a 2x2 matrix, also Array{Float64,2}.

The problem is: the result from X'SigX is of Array{Float64,2} type, although having just one element. And I can't sum it to b.

I Know I can put a dot and make the sum, but if I do that, I will still have this array type, and will be storing that on the other Array, which can be a big mess.

Any ideas to solve it keeping it simple?

Upvotes: 1

Views: 142

Answers (1)

Michael K. Borregaard
Michael K. Borregaard

Reputation: 8044

Julia explicitly distinguishes between elements and arrays of elements. Some other languages, less explicit about types (like R and Matlab), treat a single-element Matrix as a number, and users get used to doing that implicitly. In Julia, however, these two things are fundamentally different - one is a container of numbers, the other is a number.

So the answer is simply, do (X'*Sig)[1] or first(X'*Sig) to get out the element. On coming versions of Julia you should be able to do only(X'*Sig) explicitly for containers with only one element.

It is simple and clear, just different from Matlab or R. There's actually a fundamental philosophical difference of what "simple" means in the languages. In R or Matlab I'd say "simple" means "not too many operation/characters". In Julia "simple" means "clear, explicit, straightforward to reason about". It's just a different philosophy that will take some time adapting to.

Upvotes: 4

Related Questions