MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   向量化连续幂矩阵的创建 (https://www.labfans.com/bbs/showthread.php?t=23991)

poster 2019-12-10 20:48

向量化连续幂矩阵的创建
 
令x=1:100和N=1:10 。我想创建矩阵x^N使得i 列包含条目[1 ii^2 ... i^N]

我可以使用for循环轻松完成此操作。但是有办法使用矢量化代码吗?



[B]回答:[/B]

我会去:

x = 1:100; N = 1:10; Solution = repmat(x,[length(N)+1 1]).^repmat(([0 N])',[1 length(x)]); 另一个解决方案(可能效率更高):

Solution = [ones(size(x)); cumprod(repmat(x,[length(N) 1]),1)]; 甚至:

Solution = bsxfun(@power,x,[0 N]'); 希望这可以帮助。



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


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

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