How to calculate corners based on min and max
5 views (last 30 days)
Show older comments
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
Answers (1)
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
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{:});
See Also
Categories
Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!