使用 isreal 结果不一致
举这个简单的例子:
a = [1 2i];
x = zeros(1,length(a));
for n=1:length(a)
x(n) = isreal(a(n));
end
在尝试对代码进行矢量化时,我尝试了:
y = arrayfun(@isreal,a);
但结果并不相同:
x =
1 0
y =
0 0
我做错了什么?
Take this simple example:
a = [1 2i];
x = zeros(1,length(a));
for n=1:length(a)
x(n) = isreal(a(n));
end
In an attempt to vectorize the code, I tried:
y = arrayfun(@isreal,a);
But the results are not the same:
x =
1 0
y =
0 0
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这当然看起来是一个错误,但这里有一个解决方法:
为什么这有效?我不太确定,但似乎当您在调用 ISREAL 如果虚数分量为零,它会从数组元素中删除“complex”属性。在命令行窗口中尝试一下:
显然, ARRAYFUN 必须在内部维护传递给 ISREAL 的数组元素的“复杂”属性,因此即使虚部为零,也将它们全部视为复数。
This certainly appears to be a bug, but here's a workaround:
Why does this work? I'm not totally sure, but it appears that when you perform an indexing operation on the variable before calling ISREAL it removes the "complex" attribute from the array element if the imaginary component is zero. Try this in the Command Window:
Apparently, ARRAYFUN must internally maintain the "complex" attribute of the array elements it passes to ISREAL, thus treating them all as being complex numbers even if the imaginary component is zero.
了解 MATLAB 分别存储矩阵的实数/复数部分可能会有所帮助。请尝试以下操作:
其中 pr 是指向包含所有值的实部的内存块的指针,而 pi 是指向矩阵中所有值的复数部分的指针。由于所有元素都存储在一起,因此在这种情况下它们都有一个复杂的部分。
现在比较这两种方法:
与
因此,当您在 for 循环中访问
a(1)
时,返回的值(在ans
变量中)的复数似乎为零-part (nullpi
),因此被认为是真实的。另一方面,ARRAYFUN 似乎直接访问矩阵的值(而不在 ANS 变量中返回它们),因此它可以访问 pr 和 pi 指针,这两个指针不为空,因此所有元素都被认为是非真实的。
请记住这只是我的解释,我可能会弄错......
It might help to know that MATLAB stores the real/complex parts of a matrix separately. Try the following:
where
pr
is a pointer to the memory block containing the real part of all values, andpi
pointer to the complex part of all values in the matrix. Since all elements are stored together, then in this case they all have a complex part.Now compare these two approaches:
versus
So it seems that when you access
a(1)
in the for loop, the value returned (in theans
variable) has a zero complex-part (nullpi
), thus is considered real.One the other hand, ARRAYFUN seems to be directly accessing the values of the matrix (without returning them in ANS variable), thus it has access to both
pr
andpi
pointers which are not null, thus are all elements are considered non-real.Please keep in mind this just my interpretation, and I could be mistaken...
回答这个问题真的很晚了……MATLAB 函数 ISREAL 在许多用途上以一种非常违反直觉的方式运行。它告诉您给定的数组作为一个整体是否根本没有复杂的部分 - 它告诉您有关存储的信息,它并没有真正告诉您有关<的任何信息em>数组中的值。在这方面,它有点像 ISSPARSE 函数。例如,
您会在 MATLAB 中发现某些运算会自动修剪所有全零的虚部。简而言之
,MATLAB 确实需要一个函数,以数组上的元素方式回答“这个值的虚部是否为零”的问题。
Answering really late on this one... The MATLAB function ISREAL operates in a really rather counter-intuitive way for many purposes. It tells you if a given array taken as a whole has no complex part at all - it tells you about the storage, it doesn't really tell you anything about the values in the array. It's a bit like the ISSPARSE function in that regard. So, for example
What you'll find in MATLAB is that certain operations automatically trim any all-zero imaginary parts. So, for example
In short, MATLAB really needs a function which answers the question "does this value have zero imaginary part", in an elementwise way on an array.