From: "Eli " <>
Newsgroups: comp.soft-sys.matlab
Subject: Re: points in voxels
Date: Thu, 19 Nov 2009 03:03:03 +0000 (UTC)
Organization: Ryerson University
Lines: 27
Message-ID: <he2ch7$7gm$>
References: <he23gu$5bv$> <>
Reply-To: "Eli " <>
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: 1258599783 7702 (19 Nov 2009 03:03:03 GMT)
NNTP-Posting-Date: Thu, 19 Nov 2009 03:03:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 842716
Xref: comp.soft-sys.matlab:586299

ImageAnalyst <> wrote in message <>...
> On Nov 18, 7:29?pm, "Eli " <> wrote:
> > Dear Matlab users,
> > I am trying to write a program that could efficiently determine which voxel a point in space lies inside.
> > Supposing I have a 3 dimensional cube in space, that is segmented into n^3 smaller voxels. Each of these voxels will have an index ranging from 1:n^3 and each voxel centre can be determined easily.
> > No suppose I take p number of random points with coordinates (Xp,Yp,Zp) located somewhere inside the big cube.
> > I would really appreciate suggestions to writing a very efficient code to determine which voxel index each of these points lie inside.
> > At this point I have an array Mapper, with dimensions (n^3,3) indicating the centre of each small voxel.
> > Thanks very much,
> > -Eli
> ---------------------------------------------------------------------------------------
> You need to use the sub2ind() function.  For example:
> workspace; % Show the Workspace panel.
> voxels = zeros(3,4,5) % Create a 3D array - I don't care what values
> it has.
> % Find index of voxel at (x,y,z) = (1,1,1).
> index1 = sub2ind(size(voxels), 1,1,1)
> % Find index of voxel at (x,y,z) = (1,2,4).
> index2 = sub2ind(size(voxels), 1,2,4)
> % Find index of voxel at (x,y,z) = (3,4,5).
> index3 = sub2ind(size(voxels), 3,4,5)

thanks for the help, but I think you misunderstood my problem.
I am able to index the voxels. but suppose I choose a random point (x,y,z), which will not in general have integer values, and will not in general lie in the centre of the voxel. I wish to find which voxel this point falls in. What I am doing now. is calculating the difference between my point and the centres of all voxels, and then finding the minimum distance, however, this is quite computationally heavy. Is there a faster way to determine in which voxel index my point falls?