Convert list of indices to array

14 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Cris LaPierre
Cris LaPierre il 20 Set 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 Commento
Christopher Kube
Christopher Kube il 20 Set 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);

Accedi per commentare.

Più risposte (1)

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

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Tag

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by