문제

First time looking at Julia

julia> x=[1 2 3];
julia> x[2]=3+5im

ERROR: InexactError()
 in convert at complex.jl:18
 in setindex! at array.jl:346

I am sure this is because julia typing system is different.

How would one do this below in Julia?

x=[1 2 3];
x(2)=3+5*1i

x =
   1.0000 + 0.0000i   3.0000 + 5.0000i   3.0000 + 0.0000i
도움이 되었습니까?

해결책

You can make x a complex array:

x=[1 2 3];
x=complex(x);

Now you can perform this operation:

x[2]=3+5im;

This results in x containing:

println(x)

This outputs:

 1+0im 3+5im 3+0im

As desired.

다른 팁

You probably want x to be complex. In which case, you can do this:

x = Complex{Float64}[1, 2, 3]

Which allows you to do what you want. You can also change Float64 to something else like Int or Int64.

Also, you should put commas after entries to get 1-dimensional arrays instead of 2-dimensional arrays, which is what yours are. To find the type do this

typeof(x) 

which gives

1x3 Array{Complex{Float64},1}:
 1.0+0.0im  2.0+0.0im  3.0+0.0im

The 1 at the end indicates that this is a 1-dimensional array.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top