如何在Matlab中格式化复数以输出文本 [英] How do you format complex numbers for text output in matlab

查看:1067
本文介绍了如何在Matlab中格式化复数以输出文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复数,我想使用fprintf命令将其输出为文本.我看不到复数的formatspec.有没有简单的方法可以做到这一点?

I have a complex number that I want to output as text using the fprintf command. I don't see a formatspec for complex numbers. Is there an easy way to do this?

使用定点格式规范只会输出实部.

Using the format spec for fixed-point is only outputting the real part.

推荐答案

根据 sprintf

数字转换仅打印复数的实部.

Numeric conversions print only the real component of complex numbers.

因此对于复杂的值z,您可以使用此

So for a complex value z you can use this

sprintf('%f + %fi\n', z, z/1i)

例如

>> z=2+3i; 
>> sprintf('%f + %fi\n', z, z/1i)
2.000000 + 3.000000i

您可以将其包装在匿名函数中,以字符串形式轻松获取

You can wrap it in an anonymous function to get it easily as a string

zprintf = @(z) sprintf('%f + %fi', z, z/1i)

然后

>> zprintf(2+3i)

ans =

2.000000 + 3.000000i

这篇关于如何在Matlab中格式化复数以输出文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆