Path: news.mathworks.com!not-for-mail From: "Eli " <elechtma@ryerson.ca> 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$1@fred.mathworks.com> References: <he23gu$5bv$1@fred.mathworks.com> <dba81928-387f-46d6-a867-e5d56468d3c8@g23g2000yqh.googlegroups.com> Reply-To: "Eli " <elechtma@ryerson.ca> NNTP-Posting-Host: webapp-02-blr.mathworks.com Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit X-Trace: fred.mathworks.com 1258599783 7702 172.30.248.37 (19 Nov 2009 03:03:03 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Thu, 19 Nov 2009 03:03:03 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 842716 Xref: news.mathworks.com comp.soft-sys.matlab:586299 ImageAnalyst <imageanalyst@mailinator.com> wrote in message <dba81928-387f-46d6-a867-e5d56468d3c8@g23g2000yqh.googlegroups.com>... > On Nov 18, 7:29?pm, "Eli " <elech...@ryerson.ca> 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? -Eli