我的问题是在Matlab中对数组进行整形。
我在Matlab中读取来自Fortran的"diegm.MAT“文件。这个数组的大小是12x3,我需要一个4x3x3。
我尝试了整形功能,但不起作用。
这是我正在读取的数组:
5 2 5
2 1 2
4 3 2
5 3 3
5 2 4
4 2 3
1 1 3
4 5 1
3 3 1
2 1 4
2 3 1
4 2 4这是我需要的数组:
val(:,:,1) =
5 1 2
2 2 5
5 4 3
2 3 3val(:,:,2) =
5 2 3
2 3 4
4 1 5
4 1 1val(:,:,3) =
3 1 1
3 4 4
1 2 2
2 3 4在这里,您可以获得我传输给Fortran的.MAT文件。
http://www.mediafire.com/file/yhcj18ampvy92t5/diegm.mat
发布于 2018-06-01 16:45:17
也许有一种更有效的方法来做到这一点,但这似乎奏效了。
Input = [
5 2 5;
2 1 2;
4 3 2;
5 3 3;
5 2 4;
4 2 3;
1 1 3;
4 5 1;
3 3 1;
2 1 4;
2 3 1;
4 2 4
];
% Make input matrix into 1x36 vector to preserve ordering
InputAsSingleRow = reshape(Input', [], 1);
% Reshape into 4x9 matrix
Output = reshape(InputAsSingleRow,[4,9]);
% Reshape into 4x3x3 matrix you wanted
Output2 = reshape(Output,[4,3,3])结果:
Output2 =
ans(:,:,1) =
5 1 2
2 2 5
5 4 3
2 3 3
ans(:,:,2) =
5 2 3
2 3 4
4 1 5
4 1 1
ans(:,:,3) =
3 1 1
3 4 4
1 2 2
2 3 4https://stackoverflow.com/questions/50628534
复制相似问题