Filling an array with recursive function

7 Ansichten (letzte 30 Tage)
mustafa ozendi
mustafa ozendi am 7 Jan. 2019
Kommentiert: mustafa ozendi am 9 Jan. 2019
Dear MATLAB Users,
I am trying to fill in the empty array using a recursive formula. The formula is shown as follows:
(This expression is the base case for resursive function)
For all
In this formula, I already know the B array which is .
So far, I had many attempts but non of them worked. My function is shown below. When I run it, 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?
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 am looking forward to hearing from you
  3 Kommentare
mustafa ozendi
mustafa ozendi am 7 Jan. 2019
B_matrix is the array computed before and I know all elements of it. Dimensions of B_matirx is MxMxM which is in my case 3X3X3. The other inputs u_ind, v_ind, w_ind indices. I call this function in my program as shown below:
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
I initialize the I_matrix which is to be filled (max_boxes =3). B_matrix is loaded in my workspace which is 3x3x3.
mustafa ozendi
mustafa ozendi am 9 Jan. 2019
Thanks for comments

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 7 Jan. 2019
Bearbeitet: Stephen23 am 7 Jan. 2019
I don't see why you need to use a recursive function:
M = 3;
B = randi(9,M,M,M);
I = zeros(M,M,M);
for ku = 1:M
for kv = 1:M
for kw = 1:M
if kw>1
I(ku,kv,kw) = I(ku,kv,kw-1) + B(ku,kv,kw-1);
elseif kv>1
I(ku,kv,kw) = I(ku,kv-1,M-1) + B(ku,kv-1,M-1);
elseif kw>1
I(ku,kv,kw) = I(ku-1,M-1,M-1) + B(ku-1,M-1,M-1);
end
end
end
end
Note that the formula you gave will give different results depending on the order that you fill the dimensions in.
  3 Kommentare
Stephen23
Stephen23 am 7 Jan. 2019
"As far as I know the more for loops in a matlab program means the more execution time"
Not really. This might have been true fifteen years ago, but MATLAB from fifteen years ago is definitely not the same as MATLAB today. Loops are very effiicient and there is absolutely no reason to avoid using them. Just make sure that you preallocate as required!
"That is the reason why I am triyng to implement it as a recursive function"
Recursive functions have a lot more overhead than a few loops, so your approach is likely to be much slower. Test it yourself to find out!
"I realized that this problem can only be solved with a recursive fucntion but I will definitely try your solution"
I am confused: if it can only be solved using a recursive function, why bother trying my loops?
mustafa ozendi
mustafa ozendi am 9 Jan. 2019
Thanks for comments

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Cell Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by