Convert list of indices to array

14 次查看(过去 30 天)
Hello,
I have a 27x4 matrix with symbolic entries, say X, as seen below.
[ 1, 1, 1, 0]
[ 2, 1, 1, 0]
[ 3, 1, 1, e31]
[ 1, 2, 1, 0]
[ 2, 2, 1, 0]
[ 3, 2, 1, 0]
[ 1, 3, 1, e15]
[ 2, 3, 1, 0]
[ 3, 3, 1, 0]
[ 1, 1, 2, 0]
[ 2, 1, 2, 0]
[ 3, 1, 2, 0]
[ 1, 2, 2, 0]
[ 2, 2, 2, 0]
[ 3, 2, 2, e32]
[ 1, 3, 2, 0]
[ 2, 3, 2, e24]
[ 3, 3, 2, 0]
[ 1, 1, 3, e15]
[ 2, 1, 3, 0]
[ 3, 1, 3, 0]
[ 1, 2, 3, 0]
[ 2, 2, 3, e24]
[ 3, 2, 3, 0]
[ 1, 3, 3, 0]
[ 2, 3, 3, 0]
[ 3, 3, 3, e33]
From this, I would like to create a 3D array, say x, in which the columns are the index values that map to the fourth column in X. For example, once x is created, I would be able to make the call x(2,2,3) and it would return the symbolic 'e24'; calling x(2,1,1) returns 0; and so forth. Thank you for your help!

采纳的回答

Cris LaPierre
Cris LaPierre 2021-9-20
You should be able to use the reshape command on the 4th column
% Setup your matrix
syms e31 e15 e32 e24 e33
a= [1, 1, 1, 0;
2, 1, 1, 0;
3, 1, 1, e31;
1, 2, 1, 0;
2, 2, 1, 0;
3, 2, 1, 0;
1, 3, 1, e15;
2, 3, 1, 0;
3, 3, 1, 0;
1, 1, 2, 0;
2, 1, 2, 0;
3, 1, 2, 0;
1, 2, 2, 0;
2, 2, 2, 0;
3, 2, 2, e32;
1, 3, 2, 0;
2, 3, 2, e24;
3, 3, 2, 0;
1, 1, 3, e15;
2, 1, 3, 0;
3, 1, 3, 0;
1, 2, 3, 0;
2, 2, 3, e24;
3, 2, 3, 0;
1, 3, 3, 0;
2, 3, 3, 0;
3, 3, 3, e33];
% reshape to a 3x3x3
x = reshape(a(:,4),3,3,3)
x(:,:,1) = 
x(:,:,2) = 
x(:,:,3) = 
x(2,2,3)
ans = 
x(2,1,1)
ans = 
0
  1 个评论
Christopher Kube
Christopher Kube 2021-9-20
Perfect! Thank you. I think this can easily be generalized as well with N being the dimension of the matrix.
x = reshape(a(:,end),ones(1,N)*3);

请先登录,再进行评论。

更多回答(1 个)

David Hill
David Hill 2021-9-20
x=reshape(yourMatrix(:,4),[3],[3],[3]);

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

产品


版本

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by