title: How to smooth a 3D matrix with a mask in matlab
body: <p>I need to smooth a 3D matrix <code>M</code>. The output of smoothing is <code>S</code>. The matlab code can be like this:</p>
  
  <pre><code>S = smooth3(M, 'box', 3); 
  </code></pre>
  
  <p>The problem is only some parts in the matrix <code>M</code> should be considered during the smoothing. I have a mask <code>A</code> to identify the location of these parts. <code>A</code> has the same size as <code>M</code>, and the value of <code>1</code> means to consider this point and <code>0</code> means to ignore this point. How to do that?</p>
  
  <p>For example, if I have <code>M</code> and <code>A</code> in the following:</p>
  
  <pre><code>M=zeros(7,7,7); M(2:6,2:6,2:6)=ones(5,5,5); M(4,4,4)=28;
  
  A=zeros(7,7,7); A(2:6,2:6,2:6)=ones(5,5,5);
  </code></pre>
  
  <p>After the smoothing, I want to have the result like this:</p>
  
  <pre><code>S=zeros(7,7,7); S(2:6,2:6,2:6)=smooth3(M(2:6,2:6,2:6), 'box', 3); 
  </code></pre>
  
  <p>Anyone knows how to do this kind of smoothing with mask in matlab? Thank you in advance.</p>