简体   繁体   中英

How to manipulate imaginary part of matrixes in MATLAB

Look:

i=[1+1i 2+2i];

I want to do this:

imag(i)=10;

Desired result:

ans = [1+10i 2+10i]


Also what about this:

m=[1+1i 2+2i 3+3i 4+4i 5+5i];

I want:

imag(m(real(m)>2)) = 10;

Desired result:

ans = [ 1+1i 2+2i 3+10i 4+10i 5+10i ]

Unfortunately! These Desired result not happening in MATLAB.

A quick note, i is bad variable name when you are using complex numbers so I will substitute it with vec

vec=[1+1i 2+2i];
vec = complex(real(vec), 2);

Edit

With logical indexing:

idn = real(vec)>2;
vec(idn) = complex(real(vec(idn)), 10);

In the first case you can do something like:

v = [1+1i 2+2i];
v = real(v) + 10i;

In the second case you can do something like:

v = [1+1i 2+2i 3+3i 4+4i 5+5i];
v(real(v)>2) = real(v(real(v)>2)) + 10i;

As far as I know, is not possible to reference real and imaginary parts of a complex number in Matlab as if they are seperate variables without a function call to real and/or imaginary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM