Convert list of indices to array

14 Ansichten (letzte 30 Tage)
Christopher Kube
Christopher Kube am 20 Sep. 2021
Kommentiert: Christopher Kube am 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!

Akzeptierte Antwort

Cris LaPierre
Cris LaPierre am 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 Kommentar
Christopher Kube
Christopher Kube am 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);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

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

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by