如何在 MATLAB 中不使用 for 循环生成这个 3-D 矩阵?

发布于 2024-10-06 21:11:31 字数 106 浏览 3 评论 0原文

我想生成一个 N×N×3 矩阵 A,使得 A(:,:,i) = eye(n)*i。如何在不使用 for 循环(即以矢量化方式)的情况下做到这一点?

I'd like to generate an N-by-N-by-3 matrix A such that A(:,:,i) = eye(n)*i. How can I do this without using for loops (i.e. in a vectorized fashion)?

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

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

发布评论

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

评论(3

入怼 2024-10-13 21:11:31

一种方法是使用函数 KRON重塑

>> N = 4;
>> A = reshape(kron(1:3,eye(N)),[N N 3])

A(:,:,1) =

     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1

A(:,:,2) =

     2     0     0     0
     0     2     0     0
     0     0     2     0
     0     0     0     2

A(:,:,3) =

     3     0     0     0
     0     3     0     0
     0     0     3     0
     0     0     0     3

One way to do this is to use the functions KRON and RESHAPE:

>> N = 4;
>> A = reshape(kron(1:3,eye(N)),[N N 3])

A(:,:,1) =

     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1

A(:,:,2) =

     2     0     0     0
     0     2     0     0
     0     0     2     0
     0     0     0     2

A(:,:,3) =

     3     0     0     0
     0     3     0     0
     0     0     3     0
     0     0     0     3
べ映画 2024-10-13 21:11:31

另一种选择是使用 BSXFUN,将单位矩阵乘以 1- 1,2,3 的 by-1×3 数组

>> bsxfun(@times,eye(4),permute(1:3,[3,1,2]))
ans(:,:,1) =
     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1
ans(:,:,2) =
     2     0     0     0
     0     2     0     0
     0     0     2     0
     0     0     0     2
ans(:,:,3) =
     3     0     0     0
     0     3     0     0
     0     0     3     0
     0     0     0     3

Another option is to use BSXFUN, multiplying the identity matrix with a 1-by-1-by-3 array of 1,2,3

>> bsxfun(@times,eye(4),permute(1:3,[3,1,2]))
ans(:,:,1) =
     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1
ans(:,:,2) =
     2     0     0     0
     0     2     0     0
     0     0     2     0
     0     0     0     2
ans(:,:,3) =
     3     0     0     0
     0     3     0     0
     0     0     3     0
     0     0     0     3
倾其所爱 2024-10-13 21:11:31

如果您在引入 BSXFUN 之前拥有旧版本的 MATLAB,请考虑此选项(与 @Jonas):

N = 4; M = 3;
A = repmat(eye(N),[1 1 M]) .* repmat(permute(1:M,[3 1 2]),[N N 1])

A(:,:,1) =
     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1
A(:,:,2) =
     2     0     0     0
     0     2     0     0
     0     0     2     0
     0     0     0     2
A(:,:,3) =
     3     0     0     0
     0     3     0     0
     0     0     3     0
     0     0     0     3

If you have an older version of MATLAB before BSXFUN was introduced, consider this option (the same answer as the one by @Jonas):

N = 4; M = 3;
A = repmat(eye(N),[1 1 M]) .* repmat(permute(1:M,[3 1 2]),[N N 1])

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