3D-matrix

4 views (last 30 days)
Lizan
Lizan on 15 Aug 2011
Can an 3D matrix store the coordinates (x,y,z) for each value in the matrix. Something like,
M_coord = [ {1,1,1}, {1,2,1}, {1,3,1}; {2,1,1}, {2,2,1}, {2,3,1}; {3,1,1}, {3,2,1}, {3,3,1} ];
  1 Comment
Jan
Jan on 15 Aug 2011
@Susan: Please read this again: http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer , http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup , http://www.mathworks.com/matlabcentral/answers/728-how-do-i-write-a-good-question-for-matlab-answers.

Sign in to comment.

Accepted Answer

Jan
Jan on 15 Aug 2011
A "matrix" is 2D. In consequence a "3D matrix" cannot store anything.
As long, as you do not specify exactly, what you want, it is impossible to give a valuable answer. But of course I can guess, that you want a CELL matrix:
M_coord = {[1,1,1], [1,2,1], [1,3,1]; ...
[2,1,1], [2,2,1], [2,3,1]; ...
[3,1,1], [3,2,1], [3,3,1]}
Or perhaps a 3D array:
M_coord = cat(3, [1,1,1; 2,1,1; 3,1,1], ...
[1,2,1; 2,2,1; 3,2,1], ...
[1,3,1; 2,3,1; 3,3,1]);

More Answers (1)

Walter Roberson
Walter Roberson on 15 Aug 2011
There are disagreements in terminology as to what a "matrix" is. My background is sufficiently different than Jan's that I have no problem talking about a "3D Matrix".
Here is a generalization for larger sizes. Let M, N, and P be the dimensions you want:
[mg, ng, pg] = ndgrid(1:M, 1:N, 1:P);
M_coord = arrayfun(@(m,n,p) {[m,n,p]}, mg, ng, pg);
Then, e.g., M_coord{2,1,4}

Tags

Community Treasure Hunt

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

Start Hunting!