MATLAB中复数的比较(Comparison of complex numbers in MATLAB)

编程入门 行业动态 更新时间:2024-10-26 02:35:54
MATLAB中复数的比较(Comparison of complex numbers in MATLAB)

我试图弄清楚MATLAB如何使用以下代码比较复数。 我不确定这是否是预期的行为,或者我是否发现了一个错误。

max的文档说明如下:

当X很复杂时,使用幅度MAX(ABS(X))计算最大值。 在相等幅度元件的情况下,则使用相位角MAX(ANGLE(X))。

max的行为与预期的文档匹配。

>> a = complex(rand(3,1), rand(3,1)) a = 0.8147 + 0.9134i 0.9058 + 0.6324i 0.1270 + 0.0975i >> b = complex(imag(a), real(a)) b = 0.9134 + 0.8147i 0.6324 + 0.9058i 0.0975 + 0.1270i >> max(a, b) ans = 0.8147 + 0.9134i 0.6324 + 0.9058i 0.0975 + 0.1270i >> a > b ans = 0 1 1 >> angle(a) > angle(b) ans = 1 0 0 >> abs(a) == abs(b) ans = 1 1 1

但是当我尝试使用大于运算符“>”时,matlab似乎只使用真实部分进行比较。

>> a = complex(rand(5,1), rand(5,1)) a = 0.1576 + 0.1419i 0.9706 + 0.4218i 0.9572 + 0.9157i 0.4854 + 0.7922i 0.8003 + 0.9595i >> b = complex(imag(a), real(a)) b = 0.1419 + 0.1576i 0.4218 + 0.9706i 0.9157 + 0.9572i 0.7922 + 0.4854i 0.9595 + 0.8003i >> max(a, b) == a ans = 0 0 0 1 1 >> a > b ans = 1 1 1 0 0 >> real(a) > real(b) ans = 1 1 1 0 0

行为是否有任何特殊原因从max变为> ?

I am trying to figure out how MATLAB compares complex numbers using the following code. I am not sure if this is expected behavior or if I have uncovered a bug.

The documentation for max says the following:

When X is complex, the maximum is computed using the magnitude MAX(ABS(X)). In the case of equal magnitude elements, then the phase angle MAX(ANGLE(X)) is used.

The behavior of max matches the documentation as expected.

>> a = complex(rand(3,1), rand(3,1)) a = 0.8147 + 0.9134i 0.9058 + 0.6324i 0.1270 + 0.0975i >> b = complex(imag(a), real(a)) b = 0.9134 + 0.8147i 0.6324 + 0.9058i 0.0975 + 0.1270i >> max(a, b) ans = 0.8147 + 0.9134i 0.6324 + 0.9058i 0.0975 + 0.1270i >> a > b ans = 0 1 1 >> angle(a) > angle(b) ans = 1 0 0 >> abs(a) == abs(b) ans = 1 1 1

However when I try to use greater than operator, ">", matlab seems to use just the real part for comparison.

>> a = complex(rand(5,1), rand(5,1)) a = 0.1576 + 0.1419i 0.9706 + 0.4218i 0.9572 + 0.9157i 0.4854 + 0.7922i 0.8003 + 0.9595i >> b = complex(imag(a), real(a)) b = 0.1419 + 0.1576i 0.4218 + 0.9706i 0.9157 + 0.9572i 0.7922 + 0.4854i 0.9595 + 0.8003i >> max(a, b) == a ans = 0 0 0 1 1 >> a > b ans = 1 1 1 0 0 >> real(a) > real(b) ans = 1 1 1 0 0

Is there any particular reason the behavior changes in this manner from max to > ?

最满意答案

这是来自

doc >

该测试仅比较数值数组的实际部分

碰巧的是,>的实现只关注真实部分。 Matlab团队的设计决策似乎是合法的。

涉及比较运算符的绝大多数操作都旨在使用实数。 为诸如>之类的基本操作添加特殊行为以处理复杂数字将导致90%的代码不需要它的大量命中。 特别是,没有标准的方法来比较复数。 这取决于您的应用程序。

This is from

doc >

The test compares only the real part of numeric arrays

It so happens that the implementations of > looks only at the real part. The design decision from the Matlab team seems legit.

The overwhelming majority of operations that involve the comparison operator are intended to work with real numbers. Adding a special behavior for a basic operation like > to handle complex numbers will cause a big on hit for the 90% of code that does not require it. Especially, that there is not standard way to compare complex numbers. It depends on your application.

更多推荐

本文发布于:2023-08-05 21:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1438065.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:复数   MATLAB   Comparison   numbers   complex

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!