Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add complex number to an array?

Tags:

julia

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
like image 777
Nasser Avatar asked Mar 31 '14 02:03

Nasser


People also ask

How do you add complex numbers?

To add or subtract two complex numbers, just add or subtract the corresponding real and imaginary parts. For instance, the sum of 5 + 3i and 4 + 2i is 9 + 5i. For another, the sum of 3 + i and –1 + 2i is 2 + 3i.


1 Answers

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.

like image 72
jrd1 Avatar answered Sep 25 '22 18:09

jrd1