Convert list of indices to array

조회 수: 15 (최근 30일)
Christopher Kube
Christopher Kube 2021년 9월 20일
댓글: Christopher Kube 2021년 9월 20일
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