MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   使用isreal的结果不一致 (https://www.labfans.com/bbs/showthread.php?t=23838)

poster 2019-12-10 20:48

使用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 我究竟做错了什么?



[B]回答:[/B]

当然,这似乎是一个错误,但是这里有一个解决方法:

>> y = arrayfun(@(x) isreal(x(1)),a) ans = 1 0 为什么这样做?我不能[I]完全[/I]肯定,但现在看来,当你调用[I]之前[/I]对变量执行的索引操作[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/isreal.html"]伊斯雷尔[/URL]如果虚分量为零它会从数组元素中的“复杂”属性。在命令窗口中尝试以下操作:

>> a = [1 2i]; %# A complex array >> b = a(1); %# Indexing element 1 removes the complex attribute... >> c = complex(a(1)); %# ...but we can put that attribute back >> whos Name Size Bytes Class Attributes a 1x2 32 double complex b 1x1 8 double %# Not complex c 1x1 16 double complex %# Still complex 显然, [URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/arrayfun.html"]ARRAYFUN[/URL]必须在内部维护传递给[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/isreal.html"]ISREAL[/URL]的数组元素的“ complex”属性,因此即使虚数为零,也将它们全部视为复数。



[url=https://stackoverflow.com/questions/3602587]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 01:05

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.