how can I divide a vector in imaginary and real part ?

hi,
I have a vector X=[-8,-6,-5+8i,-3,-6+6i,-2], and i want to divide it in imaginary and in real part in two vectors like this :
X_real=[-8,-6,-3,-2]
X_imag=[-5+8i,-6+6i]
any idea ? thans :)

 Risposta accettata

Wayne King
Wayne King il 24 Gen 2013
Modificato: Wayne King il 24 Gen 2013
X_real = real(X);
X_imag = imag(X);
It's better style to write the unit imaginary as 1i, so
6+6*1i
However, in your example X_imag does not just contain the imaginary part. If you really want "X_imag" to just select those elements of X with nonzero imaginary part, you can do
idx = find(imag(X) ~= 0);
X_new = X(idx);
Note I have changed the name, because X_imag is not really appropriate for what you get.

10 Commenti

thank you for your answer :)
but how kann i get the vektor X_real ? X_real contains only the real values and not the real part of the complex value !! you know what i mean ?
No, I don't know what you mean. There is no difference between the "real value" and the real part.
X_real = real(X);
gives you exactly what you indicate in your post
What sami wants is
idx = find(imag(X) == 0);
X_real = X(idx);
X_imag = X(~idx);
look : i have a vektor X=[-8 ,-6,-5+8i ,-3,-6+6i, -2],
if i use the command real(X), i get X_new = [-8,-6,-5,-3,-6,-2] , but i dont want this , i want that X_new=[-8,-6,-3,-2].
that means only the real value and not the real values of the complex value !! i hope its clear now :)
@Walter
i get a Empty matrix :'((
i get X_imag its right , but X_real is Empty matrix
Yes, Now I understand. You want only complex numbers with zero imaginary part.
Y = X(imag(X)==0);
gives you what you want. You should be careful though if you do computation you may end up with some small nonzero imaginary part due to rounding error. In that case, you might want to use something like
Y = real(X(abs(imag(X))<eps));
For example, imagine you have
X(2) = -6+1.2000e-17*1i;
then
Y = X(imag(X)==0);
does not give the result you want, but
Y = real(X(abs(imag(X))<eps));
does
I had ~= when I should have had == . I fixed my code, above.
yeeeeeeeeeeeeeeeeeeeeees :))))) i get both vektors :)) thank you very much Wayne king and you too Walter :))))))
thank you again :))

Accedi per commentare.

Più risposte (1)

% Original complex vector
X = [-8, -6, -5+8i, -3, -6+6i, -2];
% Real part of the vector
X_real = real(X);
% Imaginary part of the vector, retaining the 'i' in the output
X_imag = imag(X) * i;
% Display the results
disp('Real part:');
Real part:
disp(X_real);
-8 -6 -5 -3 -6 -2
disp('Imaginary part:');
Imaginary part:
disp(X_imag);
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 8.0000i 0.0000 + 0.0000i 0.0000 + 6.0000i 0.0000 + 0.0000i
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.

1 Commento

Your answer does not give as output what OP expects, you might wanna read the question (again).

Accedi per commentare.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by