Combine 3 matrices (3D)

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 日

0 投票

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 件のコメント

Matt
Matt 2015 年 10 月 13 日
編集済み: Matt 2015 年 10 月 13 日
Sorry my English is not that good. All coordinates are known (X, Y, Z).
Let us assume we have a cube (all dimensions given in meters). I have 8 points (1 at each corner).
The A matrix contains X coordinates. So for example it will be:
1, 1.5
1, 1.5
The B matrix contains the Y coordinates:
1, 1
1.5, 1.5
So what we have at the moment is a cube whose bottom left corner is at (1, 1) meters, and top right corner at (1.5, 1.5) meters.
The C matrix will define the coordinates in the Z dimension:
1, 1
1, 1
1st layer
1.5, 1.5
1.5, 1.5
(2nd layer)
So for example if I want to access the coordinates of the bottom left corner of the "first face", I want to just type in:
D(1, 1, 1)
And the result should be:
ans = [1, 1, 1] meters (unit is just to clarify)
If I want to access the top right corner of the same face, I want to type:
D(2, 2, 1)
And the result should be:
ans = [1.5, 1.5, 1] meters
But if I want to access the same top right corner, but on the other face, I want to type:
D(2, 2, 2)
And the result should be:
ans = [1.5, 1.5, 1.5] meters
My question is: how can I make such D matrix?
I hope it is better now.
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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2015 年 10 月 13 日

編集済み:

2015 年 10 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by