0

By using ndgrid, we can obtain the matrices representing the grid:

[Y, X, Z]=ndgrid(1:2,3:4,5:6)

Y(:,:,1) =
 1     1
 2     2

Y(:,:,2) =
 1     1
 2     2

X(:,:,1) =
 3     4
 3     4

X(:,:,2) =
 3     4
 3     4

Z(:,:,1) =
 5     5
 5     5

Z(:,:,2) =
 6     6
 6     6

However, there are actually 8 grid "points"

(3,1,5), (3,1,6), (3,2,5), (3,2,6), (4,1,5), (4,1,6), (4,2,5), (4,2,6)

How can I create a matrix of these 8 vectors (using ndgrid or not in the process)? That is,

3 1 5
3 1 6
3 2 5
3 2 6
4 1 5
4 1 6
4 2 5
4 2 6

I've seen this related question, but it uses meshgrid, which only works for two dimensions.

Community
  • 1
  • 1
user42459
  • 883
  • 3
  • 12
  • 29

2 Answers2

0

Easy. Just linearize the output from ndgrid:

[Y, X, Z]=ndgrid(1:2,3:4,5:6);
out = [X(:) Y(:) Z(:)]

If you want the same ordering as in your question, use sortrows:

out = sortrows([X(:) Y(:) Z(:)])
horchler
  • 18,384
  • 4
  • 37
  • 73
0

You just need to straighten these 3D vectors:

>> vertices = [X(:),Y(:),Z(:)]

vertices =

     3     1     5
     3     2     5
     4     1     5
     4     2     5
     3     1     6
     3     2     6
     4     1     6
     4     2     6
nirvana-msu
  • 3,877
  • 2
  • 19
  • 28