Here are some examples of how you can approach this problem:
mat = rand(10); %# A 10-by-10 array of random elements
vec = -3:3; %# To make a 7-by-7 subarray
subArray = mat(5+vec,4+vec); %# Get subarray centered on element (5,4)
vec = -2:2; %# To make a 5-by-5 subarray
subArray = mat(3+vec,3+vec); %# Get subarray centered on element (3,3)
Of course, you'll have to be wary of conditions where your subscripts extend beyond the bounds of the matrix you are indexing, and then decide how you want to deal with such a situation (i.e. only return the part of the subarray that is within the larger matrix, or pad the edges of the larger matrix using PADARRAY to avoid such a situation, etc.).
EDIT:
To generalize the above for sets of points that you want to get subarrays around, the solutions given by Jonas should work well. The only drawback is that you would need to have access to the Image Processing Toolbox. Here's a solution that will work without additional toolboxes, using a
, r
, and c
as you have defined them in the question:
blockSize = 3; %# Block size around each point
padSize = floor(blockSize/2); %# Padding size for the matrix
aPad = padarray(a,[padSize padSize],NaN); %# Pad the matrix with NaNs
rPad = r+padSize; %# Shift the row indices
cPad = c+padSize; %# Shift the column indices
blockIndices = (1:blockSize)-ceil(blockSize/2); %# Create indices for a block
getBlockFcn = @(r,c) aPad(r+blockIndices,c+blockIndices); %# Function to get
%# a block
subArrays = arrayfun(getBlockFcn,rPad,cPad,... %# Create a cell array with
'UniformOutput',false); %# one block per cell
This will give you an N-by-1 cell array subArrays
, where N is the number of points you have. If, for example, you want to get the maximum value of the subarray for point 3, you can do the following:
maxValue = max(subArrays{3}(:)); %# MAX will ignore NaN values
To take a mean of the entire subarray, you either have to remove NaN values first (using the function ISNAN), or use the function NANMEAN from the Statistics Toolbox:
mat = subArray{3}(:); %# Values from subarray
meanValue = mean(mat(~isnan(mat))); %# Mean of non-NaN values
%# Or...
meanValue = nanmean(subArrays{3}(:)); %# Mean of non-NaN values