Convert MATLAB type: from complex double to double

11.1k views Asked by At

I got a vector of type double let's say vec. When I perform for instance

  test=vec.^ 1.4623;

I end up with test as complex double type, and I am wondering if there is a way to convert test to double since I'd like to get decimal values. ps: It's worth highligthing that I am not interested in real numbers retrieved from:

 real(test)

As an illustration:

vec(2)^1.4623 = -1.8293^1.4623 =  -2.4185

but instead i got:

test(2)=vec(2)^1.4623= -0.285678007027767 - 2.40147255196906i

In a nutshell, a way of converting:

test(2) = -2.4185

Thanks

1

There are 1 answers

0
m.s. On BEST ANSWER

You can temporarily "remove" the minus sign, which causes the complex numbers:

vec = [-2.5, 2, -1.5 , 1, -0.5, 0];
sign_vec = sign(vec);
test = sign_vec.*((sign_vec.*vec).^1.4623);

This results in:

test =
      -3.8186    2.7555   -1.8092    1.0000   -0.3629         0

A generic way of your conversion involves the abs function:

>> test = (-1.8293)^1.4623
test = 
      -0.2858 - 2.4015i

>> -abs(test)
ans =
     -2.4185