How to extract a cube from a 3D volume/matrix?

if we assume the starting point of a cube is up-left corner,
3D_vol(Start_x : Start_x + SizeX-1, Start_y : Start_y + SizeY-1, Start_z : Start_z + SizeZ-1);
gives voxel values in a cube having starting points of X, Y, Z and size of each. Now, if the starting point is the *center* of the cube, how can I get the cube?
I tried the following, but it is not correct when the size of X/Y/Z is even.
3D_vol(start_x - (SizeX/2) - 1 : Start_x + (SizeX/2) - 1, ......
3D_vol is a 3d matrix.

 Accepted Answer

Use floor(SizeX/2) instead of (SizeX/2) because (SizeX/2) might have a 0.5 fractional part and fractional indexes are not allowed.

3 Comments

ML
ML on 17 May 2016
Edited: ML on 17 May 2016
Hi, but my problem is that I can not obtain the exact size of the cube. e.g. cube size is 10x10x10 and center point of the cube is [20 20 20] so, after applying above code, first term (X dimension) would be 20 - (10/2) -1 : 20 + (10/2) -1 which is equal to 14:24. So,I get 11 as the lenght of X but as I entioned, I need a cube 10x10x10. of course, if I subtract one side with one more -1, then the size (10) is correct, but the center point is not in center anymore.
You cannot have a center point resolve as a voxel in even cube sizes. You can either have an 11x11x11 cube with a center voxel, or 10x10x10 with a center node(corner).
If you want the values "in between" voxel locations, you'd need to do a 3-D interpolation. Try looking into interp3(). Sorry - I don't have a demo for you but there are examples.

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolating Gridded Data in Help Center and File Exchange

Asked:

ML
on 17 May 2016

Commented:

on 17 May 2016

Community Treasure Hunt

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

Start Hunting!