get p*(q*m) matrix from m*n matrix and p*q indexing matrix

1 Ansicht (letzte 30 Tage)
hey everyone,
is there an elegant way to get the following:
C=rand(8,n) %given
index=[...
1 5 1 2 3 4
2 6 2 3 4 1
3 7 6 7 8 5
4 8 5 6 7 8
1 5 1 2 3 4];
X=[...
C(1,1) C(5,1) C(1,1) ... ... C(4,1) C(1,n) C(5,n) C(1,n) ... ... C(4,n)
C(2,1) C(6,1) C(2,1) ... ... C(1,1) C(2,n) C(6,n) C(2,n) ... ... C(1,n)
C(3,1) C(7,1) C(6,1) ... ... C(5,1) ... C(3,n) C(7,n) C(6,n) ... ... C(5,n)
C(4,1) C(8,1) C(5,1) ... ... C(8,1) C(4,n) C(8,n) C(5,n) ... ... C(8,n)
C(1,1) C(5,1) C(1,1) ... ... C(4,1) C(1,n) C(5,n) C(1,n) ... ... C(4,n)
]
X=C(index,:)
gives the information but not arranged as desired. it is for plotting cubes by edge coordinates in 3d with patch command.
thanks for any help!

Akzeptierte Antwort

Davide Masiello
Davide Masiello am 17 Okt. 2022
Bearbeitet: Davide Masiello am 17 Okt. 2022
Something like this?
n = 3;
C = rand(8,n)
C = 8×3
0.8036 0.1626 0.1875 0.8851 0.4522 0.1023 0.8075 0.3073 0.0316 0.3548 0.5842 0.8018 0.6769 0.1753 0.1741 0.8239 0.0872 0.1524 0.0985 0.8049 0.5961 0.6590 0.9066 0.9541
index=[...
1 5 1 2 3 4
2 6 2 3 4 1
3 7 6 7 8 5
4 8 5 6 7 8
1 5 1 2 3 4];
X = reshape(C(index(:),1:n),size(index,1),size(index,2)*n)
X = 5×18
0.8036 0.6769 0.8036 0.8851 0.8075 0.3548 0.1626 0.1753 0.1626 0.4522 0.3073 0.5842 0.1875 0.1741 0.1875 0.1023 0.0316 0.8018 0.8851 0.8239 0.8851 0.8075 0.3548 0.8036 0.4522 0.0872 0.4522 0.3073 0.5842 0.1626 0.1023 0.1524 0.1023 0.0316 0.8018 0.1875 0.8075 0.0985 0.8239 0.0985 0.6590 0.6769 0.3073 0.8049 0.0872 0.8049 0.9066 0.1753 0.0316 0.5961 0.1524 0.5961 0.9541 0.1741 0.3548 0.6590 0.6769 0.8239 0.0985 0.6590 0.5842 0.9066 0.1753 0.0872 0.8049 0.9066 0.8018 0.9541 0.1741 0.1524 0.5961 0.9541 0.8036 0.6769 0.8036 0.8851 0.8075 0.3548 0.1626 0.1753 0.1626 0.4522 0.3073 0.5842 0.1875 0.1741 0.1875 0.1023 0.0316 0.8018
  2 Kommentare
Daniel Neubauer
Daniel Neubauer am 17 Okt. 2022
thanks for the reply.
however, is there a way without looping? my data is rather big so i'd prefer not to loop.
Davide Masiello
Davide Masiello am 17 Okt. 2022
I understand, I have changed my answer.
I believe it should work that way either.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by