How to write this script in complex conjugate, it should have real and imaginary values

Results should have real and imaginary part in the form of (a+bi)
% Initilization
r1source = zeros(length(y),length(x),length(z)) ;
r2source = zeros(length(y),length(x),length(z)) ;
r3source = zeros(length(y),length(x),length(z)) ;
for m=1:length(y)
for n=1:length(x)
for o=1:length(z)
r1source(m,n,o)=sqrt((x(n)-x1)^2+(y(m)-y1)^2+(z(o)-z1)^2);
r2source(m,n,o)=sqrt((x(n)-x2)^2+(y(m)-y2)^2+(z(o)-z2)^2);
r3source(m,n,o)=sqrt((x(n)-x3)^2+(y(m)-y3)^2+(z(o)-z3)^2);
end
end
end

4 Comments

It will return compex numbers with imaginary components only if there are negative values within the square root. Please review the concept of imaginary numbers to understand how they are created.
ok lets say from the this concept
example:
a=5
b=6
c=a+bi
real(c)
img(c)
how do i write this:
r1source(m,n,o)=sqrt((x(n)-x1)^2+(y(m)-y1)^2+(z(o)-z1)^2);
How do the a/b/c values map to the values in the last line?
r1source(m,n,o)=sqrt((x(n)-x1)^2+(y(m)-y1)^2+(z(o)-z1)^2);
if x and x1 and y and y1 and z and z1 are all real-valued, then (x(n)-x1) and (y(m)-y1) and (z(o)-z1) will be real-valued, and the square of a real quantity is never negative, so the sum of squares would never be negative, so r1source would never be complex-valued.
For r1source to be complex-valued, at least one of the quantities would have to be complex-valued. If some of the quantities are real-valued but others are purely imaginary, then although the squares of the purely imaginary components would be negative, they might not be negative enough to balance the other parts, so you could end up with sqrt() of a positive number.

Sign in to comment.

 Accepted Answer

r1source = sqrt( (reshape(y,[],1)-y1).^2) + (reshape(x,1,[])-x1).^2 + (reshape(z,1,1,[])-z1).^2 );

More Answers (0)

Community Treasure Hunt

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

Start Hunting!