向量化连续幂矩阵的创建

发布于 2024-09-27 05:38:14 字数 190 浏览 9 评论 0原文

x=1:100N=1:10。我想创建一个矩阵x^N,以便ith列包含条目[1 ii^2 .. .i^N]

我可以使用 for 循环轻松地做到这一点。但是有没有办法使用矢量化代码来做到这一点?

Let x=1:100 and N=1:10. I would like to create a matrix x^N so that the ith column contains the entries [1 i i^2 ... i^N].

I can easily do this using for loops. But is there a way to do this using vectorized code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

剩余の解释 2024-10-04 05:38:14

我会选择:

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]');

希望这会有所帮助。

I'd go for:

x = 1:100;
N = 1:10;
Solution = repmat(x,[length(N)+1 1]).^repmat(([0 N])',[1 length(x)]);

Another solution (probably much more efficient):

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

Or even:

 Solution = bsxfun(@power,x,[0 N]');

Hope this helps.

一口甜 2024-10-04 05:38:14

听起来像范德蒙德矩阵。因此,请使用 vander

A = vander(1:100);
A = A(1:10, :);

Sounds like a Vandermonde matrix. So use vander:

A = vander(1:100);
A = A(1:10, :);
尐籹人 2024-10-04 05:38:14

由于您的矩阵不是那么大,因此最直接的方法是使用 MESHGRID逐元素幂运算符 .^

[x,N] = meshgrid(1:100,0:10);
x = x.^N;

这将创建一个 11×100 矩阵,其中每列 i 包含 [i^0;我^1;我^2; ...我^10]

Since your matrices aren't that big, the most straight-forward way to do this would be to use MESHGRID and the element-wise power operator .^:

[x,N] = meshgrid(1:100,0:10);
x = x.^N;

This creates an 11-by-100 matrix where each column i contains [i^0; i^1; i^2; ... i^10].

半枫 2024-10-04 05:38:14

不确定它是否真的符合你的问题。

bsxfun(@power, cumsum(ones(100,10),2), cumsum(ones(100,10),1))

编辑:
正如阿德里安所指出的,我的第一次尝试不符合OP问题。

xn = 100;
N=10;
solution = [ones(1,xn); bsxfun(@power, cumsum(ones(N,xn),2), cumsum(ones(N,xn),1))];

Not sure if it really fits your question.

bsxfun(@power, cumsum(ones(100,10),2), cumsum(ones(100,10),1))

EDIT:
As pointed out by Adrien, my first attempt was not compliant with the OP question.

xn = 100;
N=10;
solution = [ones(1,xn); bsxfun(@power, cumsum(ones(N,xn),2), cumsum(ones(N,xn),1))];
不必了 2024-10-04 05:38:14

为什么不使用易于理解的 for 循环呢?

c = [1:10]'; %count to 100 for full scale problem
for i = 1:4; %loop to 10 for full scale problem
    M(:,i) = c.^(i-1)
end

需要更多的思考才能理解人们所展示的这段代码的巧妙矢量化版本。我的做法更像是一种野蛮的方式,但任何读过它的人都会理解它。

我更喜欢易于理解的代码。

(是的,我可以预先分配。对于像这样的小情况,不值得降低清晰度。)

Why not use an easy to understand for loop?

c = [1:10]'; %count to 100 for full scale problem
for i = 1:4; %loop to 10 for full scale problem
    M(:,i) = c.^(i-1)
end

It takes more thinking to understand the clever vectorized versions of this code that people have shown. Mine is more of a barbarian way of doing things, but anyone reading it will understand it.

I prefer easy to understand code.

(yes, I could have pre-allocated. Not worth the lowered clarity for small cases like this.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文