MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   如何在MATLAB中没有for循环的情况下生成此3-D矩阵? (https://www.labfans.com/bbs/showthread.php?t=24203)

poster 2019-12-10 20:48

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



[B]回答:[/B]

如果在引入BSXFUN之前具有较旧的MATLAB版本,请考虑以下选项(与[URL="https://stackoverflow.com/questions/4392021/matlab-how-to-generate-3d-matrix-with-this-sequence/4392223#4392223"]@Jonas给出的[/URL]答案相同):

N = 4; M = 3; A = repmat(eye(N),[1 1 M]) .* repmat(permute(1:M,[3 1 2]),[NN 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

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


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

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