Path: news.mathworks.com!not-for-mail 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$1@newscl01ah.mathworks.com> References: <he23gu$5bv$1@fred.mathworks.com> Reply-To: <HIDDEN> NNTP-Posting-Host: www-03-blr.mathworks.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: newscl01ah.mathworks.com 1332436332 27084 172.30.248.48 (22 Mar 2012 17:12:12 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Thu, 22 Mar 2012 17:12:12 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 1187260 Xref: news.mathworks.com comp.soft-sys.matlab:761861 "Eli " <elechtma@ryerson.ca> wrote in message <he23gu$5bv$1@fred.mathworks.com>... > 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