From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: points in voxels
Date: Thu, 22 Mar 2012 17:12:12 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 15
Message-ID: <jkfmhc$qec$>
References: <he23gu$5bv$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1332436332 27084 (22 Mar 2012 17:12:12 GMT)
NNTP-Posting-Date: Thu, 22 Mar 2012 17:12:12 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: comp.soft-sys.matlab:761861

"Eli " <> wrote in message <he23gu$5bv$>...
> 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 haven't specified the scale factors used in your x, y, z coordinates of points within your "3 dimensional cube in space".  However, assuming the voxel cubes are all the same size, there must exist constants ax,ay,az and bx,by,bz such that ix = round(ax*Xp+bx), iy = round(ay*Yp+by), and iz = round(az*Zp+bz) give you the indices of the corresponding voxel.  Then as imageAnalyst says, you can use 'sub2ind' to find the corresponding linear index, or else you can use the expression i = ix+n*((iy-1)+n*(iz-1)) for that purpose.  (Actually you could adjust the y and z constants so as to avoid the -1 subtractions.)

  Why do you prefer the linear index to subscript indices?

Roger Stafford