Square Root Complex Numbers

Hi
I'm new to Matlab. I have a matrix of data for example 1.04,-5.84,-5.84,8.54 etc. As you can see its a mixture of positive and negative values. I have to square root all the numbers and then add the square root up.
I get an output like this: 0 + 0.2572i 1.4758 0 + 1.0258i 0 + 1.0297i .... etc
How can I add these up because when I do the sum total is displayed like so: 24.1589 +25.8258i I was expecting a real number such as 25.0414 why is it formatted like this. I am new to matlab so not sure what I need to do, works fine if my data has no negative numbers so I assume it's to do with that.
Thanks

3 Kommentare

Walter Roberson
Walter Roberson am 9 Mär. 2013
Suppose your entire data was just (say) -2 . Then what would you want as the result of the operation?
Josh
Josh am 9 Mär. 2013
I'd expect 1.41421356i which I would get but say I have -2 and 4 (1.41421356i and 2) now if I add them up I get -2 + 1.41421356 I thought I'd get 0.58578644 though. If you can see were i'm coming from
real parts are summed separately from imaginary parts , if you want add both Real and Imag sumsthen try :
r=rand(10);
p=sqrt(r);
S=sum(real(p(:)))+sum(imag(p(:)));

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Youssef  Khmou
Youssef Khmou am 9 Mär. 2013

0 Stimmen

hi Josh,
the square root of <0 number is complex number , if you only want the real part use "real" function, but if you want imaginary part, use "imag" function, and for the modulus you can use "abs" function , example :
r=randn(10);
p=sqrt(r);
Real_part=sum(real(p(:)));
Imag_part=sum(imag(p(:)));
Modul_part=sum(abs(p(:)));
Walter Roberson
Walter Roberson am 10 Mär. 2013
Bearbeitet: Walter Roberson am 10 Mär. 2013

0 Stimmen

To get the results you indicate you want (which are not the results one would normally want when taking the square roots of negative numbers),
sum(sqrt(r(r>0))) - sum(sqrt(-r(r<0)))
Remember, sqrt(-A) is not -sqrt(A) but is instead 1i * sqrt(A) where 1i indicates sqrt(-1)

5 Kommentare

Yogan Sganzerla
Yogan Sganzerla am 6 Aug. 2019
Dear Walter,
I have a matrix M with complex number, as u can see the image below.
Complex Matrix.JPG
I would like to know the correct way to extract the squere root of M.
Could I use sqrm(M)?
Thank you for your attention.
Torsten
Torsten am 6 Aug. 2019
You want to get the matrix A with A^2 = M or with A.^2 = M ?
Yogan Sganzerla
Yogan Sganzerla am 6 Aug. 2019
Bearbeitet: Yogan Sganzerla am 6 Aug. 2019
A^2 = M
Torsten
Torsten am 6 Aug. 2019
A = sqrtm(M)
Yogan Sganzerla
Yogan Sganzerla am 6 Aug. 2019
Perfect! Thank you very much.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 9 Mär. 2013

Kommentiert:

am 6 Aug. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by