Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Efficient way for Creating 3D Matrix? (Cubes in a sphere)
Date: Sun, 29 Apr 2012 00:44:46 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 23
Message-ID: <jni2tu$h2v$1@newscl01ah.mathworks.com>
References: <jnhasm$e3d$1@newscl01ah.mathworks.com> <jnhcru$lka$1@newscl01ah.mathworks.com> <jnhs4e$lg8$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-04-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1335660286 17503 172.30.248.35 (29 Apr 2012 00:44:46 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 29 Apr 2012 00:44:46 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:766244

"mahdi roozbhani" <m.m.roozbahani@gmail.com> wrote in message <jnhs4e$lg8$1@newscl01ah.mathworks.com>...
> I dont know why I did many mistakes to write this little code. Here hopefully is the correct one. Pl help me with that?
> 
> sphere_voxel = [];
> e = 20;
> r = 1;
> t = linspace(-r,+r,e);
> for i = 1:e
>     for j = 1:e
>         for k= 1:e
>             if (t(i))^2 + (t(j))^2 + (t(k))^2 <= r^2
>                 lp = length(sphere_voxel);
>                 sphere_voxel(e+lp,:) = [t(i) t(j) t(k)];
>             end
>         end
>     end
> end
- - - - - - - - - - - -
 [TK,TJ,TI] = ndgrid(linspace(-r,+r,e));
 S = TI.^2+TJ.^2+TK.^2<=r^2;
 sphere_voxel = [TI(S),TJ(S),TK(S)];

Roger Stafford