Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Following the discussion in the comments, I am adding another solution. For a given point, we compute the neighboring points within a specified number of steps (radius if you will). This is shown for both the 2D and the 3D case.</p> <h1>2D matrix</h1> <pre><code>siz = [10 15]; %# matrix size p = [5 10]; %# 2D point location %# neighboring points k = 2; %# radius size [sx,sy] = ndgrid(-k:k,-k:k); %# steps to get to neighbors xy = bsxfun(@plus, p, [sx(:) sy(:)]); %# add shift xy = bsxfun(@min, max(xy,1), siz); %# clamp coordinates within range xy = unique(xy,'rows'); %# remove duplicates xy(ismember(xy,p,'rows'),:) = []; %# remove point itself %# show solution figure line(p(1), p(2), 'Color','r', ... 'LineStyle','none', 'Marker','.', 'MarkerSize',50) line(xy(:,1), xy(:,2), 'Color','b', ... 'LineStyle','none', 'Marker','.', 'MarkerSize',20) grid on, box on, axis equal axis([1 siz(1) 1 siz(2)]) xlabel x, ylabel y </code></pre> <p><img src="https://i.stack.imgur.com/xrroH.png" alt="screenshot_2D_matrix"></p> <hr> <h1>3D matrix</h1> <pre><code>siz = [10 15 8]; %# matrix size p = [5 10 4]; %# 3D point location %# neighboring points k = 2; %# radius size [sx,sy,sz] = ndgrid(-k:k,-k:k,-k:k); %# steps to get to neighbors xyz = bsxfun(@plus, p, [sx(:) sy(:) sz(:)]); %# add shift xyz = bsxfun(@min, max(xyz,1), siz); %# clamp coordinates within range xyz = unique(xyz,'rows'); %# remove duplicates xyz(ismember(xyz,p,'rows'),:) = []; %# remove point itself %# show solution figure line(p(1), p(2), p(3), 'Color','r', ... 'LineStyle','none', 'Marker','.', 'MarkerSize',50) line(xyz(:,1), xyz(:,2), xyz(:,3), 'Color','b', ... 'LineStyle','none', 'Marker','.', 'MarkerSize',20) view(3), grid on, box on, axis equal axis([1 siz(1) 1 siz(2) 1 siz(3)]) xlabel x, ylabel y, zlabel z </code></pre> <p><img src="https://i.stack.imgur.com/l7cAu.png" alt="screenshot_3D_matrix"></p> <p>HTH</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload