recursion - Issue updating 'cell' values in recursive function (MATLAB) -
i have binary tree accessed cell array in matlab (e.g. a{1}{2}
). able write recursive function (below) able access fringe nodes of tree. next task replace values @ fringe nodes other values. however, having difficulties part. every call of recursive function, values not keep new values, rather revert original ones. there simple way ensure values remain updated in function? thank you!
here code:
function findleaves(a) if(iscellstr(a) == 1) % above fringe node a{2} = 2; %change fringe node value 2 else if(length(a) == 3 || length(a) == 2) % go left findleaves(a{2}); end if (length(a) == 3) % go right findleaves(a{3}); end end end
in matlab, standard calls built-in data types value, means parameters passed protected against changes made in function. exceptions graphic handles, java handles , classes inherit handle classes type.
so, must pass output results changes in order keep calculated values.
function = findleaves(a) if(iscellstr(a) == 1) % above fringe node a{2} = 2; %change fringe node value 2 else if(length(a) == 3 || length(a) == 2) % go left a{2}=findleaves(a{2}); end if (length(a) == 3) % go right a{3}=findleaves(a{3}); end end
Comments
Post a Comment