Combine 3 matrices (3D)

1 visualizzazione (ultimi 30 giorni)
Matt
Matt il 13 Ott 2015
Modificato: Matt il 14 Ott 2015
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

Risposte (1)

Thorsten
Thorsten il 13 Ott 2015
Modificato: Thorsten il 13 Ott 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 Commenti
Thorsten
Thorsten il 13 Ott 2015
Modificato: Thorsten il 13 Ott 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)
Matt
Matt il 14 Ott 2015
Modificato: Matt il 14 Ott 2015
Thank you for your answer. I will give it a try. I think I have to go the 4D route since I have to check at every time step if the Z coordinate of every point is below a threshold, so having all the Z coordinates stored at one place will help me a lot in that rather than having to call the function (I think).
The issue for me then is to rotate that 4D matrice and compute the new points position but I fail to succeed in that yet.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by