gpt4 book ai didi

matlab - Matlab 中 5D 矩阵的线性化

转载 作者:太空宇宙 更新时间:2023-11-03 20:25:16 25 4
gpt4 key购买 nike

假设我有 Matrix A,在 MATLAB 中定义为:

A = zeros(2,2,2,2,2);

A(1,1,1,1,1) = 1;
A(1,1,1,1,2) = 2;
A(1,1,1,2,1) = 3;
A(1,1,1,2,2) = 4;
A(1,1,2,1,1) = 5;
A(1,1,2,1,2) = 6;
A(1,1,2,2,1) = 7;
A(1,1,2,2,2) = 8;

A(1,2,1,1,1) = 9;
A(1,2,1,1,2) = 10;
A(1,2,1,2,1) = 11;
A(1,2,1,2,2) = 12;
A(1,2,2,1,1) = 13;
A(1,2,2,1,2) = 14;
A(1,2,2,2,1) = 15;
A(1,2,2,2,2) = 16;

A(2,1,1,1,1) = 17;
A(2,1,1,1,2) = 18;
A(2,1,1,2,1) = 19;
A(2,1,1,2,2) = 20;
A(2,1,2,1,1) = 21;
A(2,1,2,1,2) = 22;
A(2,1,2,2,1) = 23;
A(2,1,2,2,2) = 24;

A(2,2,1,1,1) = 25;
A(2,2,1,1,2) = 26;
A(2,2,1,2,1) = 27;
A(2,2,1,2,2) = 28;
A(2,2,2,1,1) = 29;
A(2,2,2,1,2) = 30;
A(2,2,2,2,1) = 31;
A(2,2,2,2,2) = 32;

我怎样才能将这个矩阵转换成一个新的矩阵 B,使得矩阵 B 是一个向量,使得 B = [1,2,3,4...32]?

reshape 功能不起作用,我在网上找不到任何帮助。

提前致谢!

最佳答案

您有相反顺序的维度来实现您想要的。所以:只需置换维度(使用 permute )然后线性化(使用 : ):

B = permute(A, [5 4 3 2 1]);
B = B(:);

如果你想在一行中使用 reshape对于线性化:

B = reshape(permute(A, [5 4 3 2 1]), [],1);

如果你定义了(注意索引的反转)

A(1,1,1,1,1) = 1;
A(2,1,1,1,1) = 2;
A(1,2,1,1,1) = 3;
A(2,2,1,1,1) = 4;
A(1,1,2,1,1) = 5;
A(2,1,2,1,1) = 6;
A(1,2,2,1,1) = 7;
A(2,2,2,1,1) = 8;
...

尺寸将按“正确”顺序排列,然后您就不需要permute:

B = A(:);

B = reshape(A, [],1);    

关于matlab - Matlab 中 5D 矩阵的线性化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24268461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com