Matlab code snippets

ndgrid vs repmat

% get coodinates of points on a grid within a sphere
r=5; % 5 voxels radius
circle_points=[repmat([-r:r]',(2*r)^2,1) reshape(repmat(-r:r,2*r,2*r),(2*r)^3, 1) reshape(repmat(-r:r,(2*r)^3,1),(2*r)^3,1)];
%% the above statement can be simplified as:
[x,y,z]=ndgrid(-r:r,-r:r,-r:r);
circle_points=reshape(cat(4, x, y, z),(2*r+1)^3,3);
% remove points that are out of the radius range
circle_points=circle_points(sum(circle_points.^2,2)<=r^2);
 
 
matlab.txt ยท Last modified: 14.08.2010 00:16 by 91.125.83.233
Recent changes RSS feed Creative Commons License Valid XHTML 1.0 Valid CSS Driven by DokuWiki
Drupal Garland Theme for Dokuwiki