I am trying to fill an array I Using a recursive formula. The formula is shown below.
In this formula I already computed the B array and I know all the elements and M is the dimension of B array which is equal to dimension of I array. u,v and w letters indicate indices. My function is:
function I_matrix = fill_I_recursively(B_matrix, u_ind, v_ind, w_ind)
M = size(B_matrix,1);
if u_ind == 1 && v_ind == 1 && w_ind == 1
I_matrix(u_ind,v_ind,w_ind) = 0;
else
if w_ind > 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 > 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
I call this function in my program:
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
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?
答案 0 :(得分:1)
由于对您的fill_I_recursively
函数的期望不同而出现问题。
在函数定义中,输出为I_matrix
,它是一个多维数组。
在您的主程序中,fill_I_recursively
的预期输出是标量,它被填充到I_matrix
多维数组的条目中。
您应该更改两个部分之一,以使它们保持一致。
这是一个如何修改函数定义以输出标量的示例:
function I_uvw = fill_I_recursively(B_matrix, u_ind, v_ind, w_ind)
M = size(B_matrix,1);
if u_ind == 1 && v_ind == 1 && w_ind == 1
I_uvw = 0;
else
if w_ind > 1
I_uvw = fill_I_recursively(B_matrix, u_ind, v_ind, w_ind-1) + B_matrix(u_ind,v_ind,w_ind-1);
elseif v_ind > 1
I_uvw = fill_I_recursively(B_matrix, u_ind, v_ind-1, M-1) + B_matrix(u_ind,v_ind-1,M-1);
else
I_uvw = fill_I_recursively(B_matrix, u_ind-1, M-1, M-1) + B_matrix(u_ind-1,M-1,M-1);
end
end