How to calculate corners based on min and max

5 views (last 30 days)
Mats
Mats on 2 Nov 2017
Commented: Guillaume on 2 Nov 2017
If i have an n-dimensional space contained by straight walls (distinct max and min in each dimension). I need to find the position of the corner points.
For example in 3D I have the min and max values in each dimension in a 2x3 matrix. Is there any good way from that to calculate the cornerpoints of the shape that encloses that volume?
  2 Comments
Adam
Adam on 2 Nov 2017
The maxes and mins are the corner points aren't they?
Mats
Mats on 2 Nov 2017
Hi Adam,
Yes, but a good way to combine the coordinates to get each cornerpoint on one row with (x,y,z)

Sign in to comment.

Answers (1)

Jos (10584)
Jos (10584) on 2 Nov 2017
For the 3D case, try this:
mm = [0 2 ; 1 3 ; 4 5] % column 1 is minimum, column 2 is maximum, rows are [x y z]
[px,py,pz] = ndgrid(mm(1,:),mm(2,:),mm(3,:))
P = [px(:) py(:) pz(:)] % the 8 corners of a 3D cube
Flexible ND case:
mm = [0 2 ; 1 3 ; 4 5 ; -1 1 ; 10 20] ; % 5D
nd = size(mm,1) ; % number of dimensions
mmC = mat2cell(mm,ones(1,nd), 2) ;
[p{1:nd}] = ndgrid(mmC{:}) ;
P = reshape(cat(nd,p{:}),[],nd) % the 32 corners of a 5D "cube"
  1 Comment
Guillaume
Guillaume on 2 Nov 2017
To avoid problems if p already exists and is not a cell array (or a larger cell array than required) I'd use:
p = cell(1, nd);
[p{:}] = ndgrid(mmC{:});

Sign in to comment.

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!