Combine 3 matrices (3D)
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello all,
I have 3 matrices A, B, C, each has a size of 200x50x50. They are coordinates (X, Y, Z) of points that represent a cube. What I want to do is to obtain a matrix from which if I type the following command D(1, 4, 3), I will access the 1st element in the X dimension, the 4th element in the Y dimension, and the 3rd element in the Z dimension, which should give me the following answer (but this will change in my real case since the points are coordinates):
ans = [1, 4, 3]
For example my A matrix which represents the coordinates in the X axis is as follow:
1, 2, 3, 4, 5, 6, 7, 8, 9, ...
1, 2, 3, 4, 5, 6, 7, 8, 9, ...
1, 2, 3, 4, 5, 6, 7, 8, 9, ...
and so on, with the same numbers for every layer (Z dimension)
My B matrix which represents the coordinates in the Y axis is as follow:
1, 1, 1, 1, 1, 1, 1, 1, 1, ...
2, 2, 2, 2, 2, 2, 2, 2, 2, ...
3, 3, 3, 3, 3, 3, 3, 3, 3, ...
and so on, with also the same numbers for every layer (in the Z dimension)
And my C matrix has the same value for a whole layer, to represent the Z dimension:
1, 1, 1, 1, 1, 1, 1, 1, 1, ...
1, 1, 1, 1, 1, 1, 1, 1, 1, ...
1, 1, 1, 1, 1, 1, 1, 1, 1, ...
then second layer will be filled with twos, the 3rd layer with threes, and so on
So how can I combine these 3 matrices to obtain a single matrix that will give me the coordinates?
Thanks in advance
0 commentaires
Réponses (1)
Thorsten
le 13 Oct 2015
Modifié(e) : Thorsten
le 13 Oct 2015
It is not entirely clear to me what you are asking for.
1. So far you have just coordinates for X, Y, Z, presumably generated with meshgrid. But you do not have a value for certain combination of these coordinates, like D(1,4,3). So you have to figure out how X,Y and Z should be combined to yield your D, like, for example
D = X + 2*Y + Z.^2;
2. If you want a single linear index into your 3D cube, that is a different thing. You can obtain this using
ind = sub2ind(size(X), X, Y, Z)
3 commentaires
Thorsten
le 13 Oct 2015
Modifié(e) : Thorsten
le 13 Oct 2015
I see. You mean something like
x = [1 1.5];
y = x; z = x;
[A B C] = meshgrid(x,y,z);
D = cat(4, A, B, C);
You can then access, e.g.,
D(1,1,1,:)
D(2,2,2,:)
Note that D has 2*n^3 elements, if n is the number of different elements in A,B, and C. That seems to be an inefficient way to organize your data; the same information is already in your n values.
Instead of explicitely store the data in a huge 4D matrix, you could write an anonymous function
fD = @(i1, i2, i3) [x(i1) y(i2) z(i3)]
fD(1,1,1)
fD(2,2,2)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!