MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   MATLAB:为什么这种求和不能正常工作? (https://www.labfans.com/bbs/showthread.php?t=22840)

poster 2019-12-10 16:49

MATLAB:为什么这种求和不能正常工作?
 
我正在尝试在MATLAB中执行一个非常简单的求和。这是我的代码:

moment = 0; for y=1:rows, for x=1:cols, moment = moment + (x^p * y^q * Im(y,x)); end end 我想为每个迭代计算(x ^ p * y ^ q * Im(y,x))并将其添加到moment变量,但是此函数在第一次计算时返回该时刻。它似乎根本不做添加。

可能是一个愚蠢的错误,但我真的很困惑。有什么建议么?


回答:
Im是[B]uint8[/B]类型还是类似的小范围类型?尝试将这一行放在循环之前:

Im = double(Im); 顺便说一句,您可以在一行中重写代码:

moment = sum(sum( ((1:rows)'.^q * (1:cols).^p) .* double(Im) ));

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


所有时间均为北京时间。现在的时间是 23:21

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