Convert list of indices to array

16 vues (au cours des 30 derniers jours)
Christopher Kube
Christopher Kube le 20 Sep 2021
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!

Réponse acceptée

Cris LaPierre
Cris LaPierre le 20 Sep 2021
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 commentaire
Christopher Kube
Christopher Kube le 20 Sep 2021
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);

Connectez-vous pour commenter.

Plus de réponses (1)

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

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Tags

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by