Hello I’m quite new to Julia and coming from MATLAB and Python and I have some trouble with operations that should be quite common but I haven’t found any workaround/solution in the usual places (Manual, Github and here).
The problem is:
I cannot assign the result of a calculation to array at a specific index. The issue is that the type of the result is a Matrix with dimensions (1,1) which is cannot automatically converted to a scalar (why not?)
Example to reproduce:
A = rand(2,2)
B = rand(2,1)
C = rand(1,2)
r = C * A * B
y_ = zeros(10,1)
y_[1] = r
this results in:
MethodError: Cannot `convert` an object of type Array{Float64,2} to an object of type Float64
I know that I could just index r[1] but this also seems overly complicated.
Are there any better solutions to this?
Is it possible to overwrite e.g. convert()?
Probably only is the best. That also assures that there is indeed only one element.
Is it possible to overwrite e.g. convert() ?
No, don’t do that. This will probably break things as other code, in Julia itself or in packages will rely on convert working as advertised (this is called type piracy). Only add methods to convert for types which you defined.
So just to make sure I’m not missing something: In Julia a scalar are not equivalent and cannot be automatically casted to a vector/matrix with one element?
Yes. They are two completely different things. But as @Oscar_Smith pointed our above, if you use the “right” types for the rows and columns the result is a scalar.
Yes that’s true, unfortunately it’s not always possible to constrain/control the types in that way. This example e.g. is derived from using ControlSystems package and there the system object automatically uses matrices.