Combine 3 matrices (3D)

2 次查看(过去 30 天)
Matt
Matt 2015-10-13
编辑: Matt 2015-10-14
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

回答(1 个)

Thorsten
Thorsten 2015-10-13
编辑:Thorsten 2015-10-13
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 个评论
Thorsten
Thorsten 2015-10-13
编辑:Thorsten 2015-10-13
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 2015-10-14
编辑:Matt 2015-10-14
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.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by