Jesus
Jesus

Reputation: 179

How to output the real and imaginary parts separately instead of having just one complex vector

I have a function M that outputs complex numbers taking an input range of r's. Instead of just outputting a single complex number, I would like to have the function to output two values (the real and imaginary parts separately) for all the output complex vectors. I would prefer the function to be anonymous function.

I tried the following but did not work since I am just getting single output complex values:

r = linspace(1E-10,1.5,100);
%M= (0.5*((1i*r+0.135).* (1i*r+0.651)))./((1i*r+0.0965).* (1i*r+0.4555))
M= @(r)(0.5*((1i*r+0.135).* (1i*r+0.651)))./((1i*r+0.0965).* (1i*r+0.4555))

How do I separate the real and complex parts of a vector?

Upvotes: 0

Views: 694

Answers (1)

Sardar Usama
Sardar Usama

Reputation: 19689

Create an anonymous function with a different variable to avoid confusion i.e. create M with:

M = @(k)(0.5*((1i*k+0.135).* (1i*k+0.651)))./((1i*k+0.0965).* (1i*k+0.4555));

then create another anonymous function, say N, that extracts real and imag values and then stacks the result.

N = @(k) [real(M(k)); imag(M(k))];

Call this anonymous function with N(r) to get your expected result.


Alternately if you have already calculated M as in your commented out code then you can do:

N = @(k) [real(k); imag(k)];

and then call it with N(M).

Upvotes: 2

Related Questions