title: Recursively fill array in MATLAB
body: <p>I am trying to fill an array <strong><em>I</em></strong> Using a recursive formula. The formula is shown below. 
  <a href="https://i.stack.imgur.com/IePWy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IePWy.png" alt="enter image description here"></a></p>
  
  <p>In this formula I already computed the <strong><em>B</em></strong> array and I know all the elements and <strong><em>M</em></strong> is the dimension of <strong><em>B</em></strong> array which is equal to dimension of <strong><em>I</em></strong> array. <strong><em>u,v</em></strong> and <strong><em>w</em></strong> letters indicate indices. My function is:</p>
  
  <pre><code>function I_matrix = fill_I_recursively(B_matrix, u_ind, v_ind, w_ind)
  M = size(B_matrix,1);
  if u_ind == 1 &amp;&amp; v_ind == 1 &amp;&amp; w_ind == 1
      I_matrix(u_ind,v_ind,w_ind) = 0; 
  else
      if w_ind &gt; 1
          I_matrix(u_ind,v_ind,w_ind) = fill_I_recursively(B_matrix, u_ind, v_ind, w_ind-1) + B_matrix(u_ind,v_ind,w_ind-1);
      elseif v_ind &gt; 1
          I_matrix(u_ind,v_ind,w_ind) = fill_I_recursively(B_matrix, u_ind, v_ind-1, M-1) + B_matrix(u_ind,v_ind-1,M-1);
      else
          I_matrix(u_ind,v_ind,w_ind) = fill_I_recursively(B_matrix, u_ind-1,  M-1, M-1) + B_matrix(u_ind-1,M-1,M-1);
     end
  end
  </code></pre>
  
  <p>I call this function in my program:</p>
  
  <pre><code>I_matrix = NaN(max_boxes,max_boxes,max_boxes);
  %%fill the I matrix using recursive function
  for u_ind = 1:max_boxes
      for v_ind = 1:max_boxes
          for w_ind = 1:max_boxes
              I_matrix(u_ind,v_ind,w_ind) = fill_I_recursively(B_matrix, u_ind, v_ind, w_ind);
          end
      end
  end
  </code></pre>
  
  <p>When it runs I get the error message "Assignment has more non-singleton rhs dimensions than non-singleton subscripts". Could you please help me to solve this problem?</p>