faster way to generate submatrices?

Hi,
Lets assume that maxtrix A of size axbxc is given.
I want to generate another matrix from matrix A, such that each element (i,j,k) of the new matrix is formed by taking dxdxd (d<a, d<b, d<c) elements around element (i,j,k) and summing them. I can use 3 for loops, but is there any faster solution?

Answers (2)

doc convn
3-dimensional convolution
specifically:
m = neighborhood_window_size;
B = convn(A,ones(m,m,m),'valid'); %or 'full', or 'same' depending on what you want at the boundaries
Here is an example:
A = ceil(rand(4,4,4)*10)
F = @(x,y,z,d) A(max(x-d,1):min(x+d,size(A,1)),max(y-d,1):min(y+d,size(A,2)),max(z-d,1):min(z+d,size(A,3)));
F(2,3,3,1)
For an even faster response, skip the function handle and just index into A as I did in the function...

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Asked:

on 5 Jul 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!