Using fprintf for complex numbers

60 ビュー (過去 30 日間)
Savannah Roemer
Savannah Roemer 2019 年 8 月 27 日
回答済み: Steven Lord 2025 年 12 月 16 日
Hi, I have
Z1= sqrt(3) * exp((pi*i)/4)
Z2= 2 * exp((pi*i)/6)
Z3= sqrt(3) * exp((3*pi*i)/4)
Z4= 2 * exp(pi*i)
Z5= 2 * exp(-pi*i)
And I would like something like
Real Complex
Z1 1.2247 + 1.2247i
Z2 1.7321 + 1.0000i
Z3 -1.2247 + 1.2247i
Z4 -2.0000 + 0.0000i
Z5 -2.0000 - 0.0000i
Using fprintf, I would think...
Thanks for your help!

採用された回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 8 月 27 日
編集済み: KALYAN ACHARJYA 2019 年 8 月 27 日
One Way:
Z1= sqrt(3) * exp((pi*i)/4)
fprintf('%f + %fi',real(Z1),imag(Z1))
More:
Z1= sqrt(3) * exp((pi*i)/4);
Z2= 2 * exp((pi*i)/6);
Z3= sqrt(3) * exp((3*pi*i)/4);
Z4= 2 * exp(pi*i);
Z5= 2 * exp(-pi*i);
disp(' Real Complex')
fprintf('\nZ1 %.4f + %.4fi',real(Z1),imag(Z1));
fprintf('\nZ2 %.4f + %.4fi',real(Z2),imag(Z2));
fprintf('\nZ3 %.4f + %.4fi',real(Z3),imag(Z3));
fprintf('\nZ4 %.4f + %.4fi',real(Z4),imag(Z4));
fprintf('\nZ5 %.4f + %.4fi',real(Z5),imag(Z5));
Result:
678.png
  2 件のコメント
Savannah Roemer
Savannah Roemer 2019 年 8 月 27 日
You're the real MVP!
Thanks so much!
S
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 8 月 27 日
No Savannah, those MPVs are my Guru here.. continually they are teaching me .
Specially @Walter Roberson, @Image Analyst @Madhan @Jan .and...many more....
Anyway thanks for your kind words!

サインインしてコメントする。

その他の回答 (4 件)

Adam Danz
Adam Danz 2023 年 11 月 24 日
編集済み: Adam Danz 2023 年 11 月 24 日
Another alternative is to use num2str which supports conversion of complex numbers to strings.
a = -2*sqrt(-3)+5/3
a = 1.6667 - 3.4641i
num2str(a)
ans = '1.6667-3.4641i'
num2str(a, 2) % specify precision
ans = '1.7-3.5i'
You would enter this as a string in fprintf
ijstr = num2str(a,3);
fprintf('%s %s', 'Z1', ijstr)
Z1 1.67-3.46i
Note that this method may return strings in undesired formats such as,
num2str(160, 2)
ans = '1.6e+02'
  5 件のコメント
marcel hendrix
marcel hendrix 2024 年 11 月 30 日
Thanks, I was on the verge of submitting a bug report...
Processing matrix arguments by column has another surprise: if one prints a matrix to a file for subsequent processing in another program, the matrix will look transposed to the post-processor. I can say from experience that this bug (in the post-processor and caused by the reverse side of the keyboard) can be very hard to find ...
Walter Roberson
Walter Roberson 2024 年 11 月 30 日
Note that you can avoid the c{:} step if you pass compose a string scalar format instead of a character vector format.
p = [ 9.542130347090435e-01 + 1.371703909876786e-01i
9.542130347090435e-01 - 1.371703909876786e-01i ];
c = compose("%s", num2str(p,10))
c = 2x1 string array
"0.9542130347+0.137170391i" "0.9542130347-0.137170391i"
fprintf('%s\n', c)
0.9542130347+0.137170391i 0.9542130347-0.137170391i
And that leads to the more compact
fprintf('%s\n', compose("%s", num2str(p,10)))
0.9542130347+0.137170391i 0.9542130347-0.137170391i

サインインしてコメントする。


Walter Roberson
Walter Roberson 2019 年 8 月 27 日
編集済み: Walter Roberson 2019 年 8 月 27 日
fprintf() always ignores imaginary components of numbers. You need to ask to output the real() and imaginary() components separately,
fprintf('%2s %7.4f %+7.4fi\n', 'Z1', real(Z1), imag(Z1))

Dean
Dean 2025 年 12 月 16 日
x = [
2.34 + 4.35i
6.34 + 4.32i
7.45 + 3.21i
8.45 + 9.23i
];
fprintf('%0.2f + i%0.2f\r', [real(x) imag(x)].');
2.34 + i4.35 6.34 + i4.32 7.45 + i3.21 8.45 + i9.23

Steven Lord
Steven Lord 2025 年 12 月 16 日
You may want to use + in the format specifier.
A = [3+4i, 5, 6-7i]
A =
3.0000 + 4.0000i 5.0000 + 0.0000i 6.0000 - 7.0000i
fprintf("%d%+di\t", [real(A); imag(A)])
3+4i 5+0i 6-7i

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by