I know its possible in Java and C++ etc, but is this possible in Matlab?
I’ve recently discovered Matlab doesnt have short cuts like value++ instead they have to use value = value+1 so i’m wondering whether its possible to convert this function to an iterative one. I’m not sure where to start. If so, is it less beneficial than a recursive function?
function [lines] = recurse(R,C, lines, T_l, count, directions)
[rows, columns] = size(lines);
if((R < 2 || C < 2) || (R > rows-1 || C > columns - 1) || count>500)
count= count+1;
return;
end
count= count+1;
direction = directions(R,C);
if(direction >= 68 || direction <=-68)
if(lines(R-1,C) > T_l)
lines(R-1,C) = 0;
lines = recurse(R-1,C, lines, T_l, count, directions);
end
if(lines(R+1,C) > T_l)
lines(R+1,C) = 0;
lines = recurse(R+1,C, lines, T_l, count, directions);
end
elseif (direction <= -23 && direction >-68)
if(lines(R+1,C+1) > T_l)
lines(R+1,C+1) = 0;
lines = recurse(R+1,C+1, lines, T_l, count, directions);
end
if(lines(R-1,C-1) > T_l)
lines(R-1,C-1) = 0;
lines = recurse(R-1,C-1, lines, T_l, count, directions);
end
elseif (direction >= 23 && direction < 68)
if(lines(R+1,C-1) > T_l)
lines(R+1,C-1) = 0;
lines = recurse(R+1,C-1, lines, T_l, count, directions);
end
if(lines(R-1,C+1) > T_l)
lines(R-1,C+1) = 0;
lines = recurse(R-1,C+1, lines, T_l, count, directions);
end
else
if(lines(R,C+1) > T_l)
lines(R,C+1) = 0;
lines = recurse(R,C+1, lines, T_l, count, directions);
end
if(lines(R,C-1) > T_l)
lines(R,C-1) = 0;
lines = recurse(R,C-1, lines, T_l, count, directions);
end
end
lines(R,C) = 255;
return;
Basically, i currently have a function that calls this second function recursively. I was hoping to consolidate this recursive function into the function calling it as a iterative set of commands.
I’m pretty sure it will be slower but speed isn’t an issue for me and i’m interested to see how the loops will work. Thanks.
You can accomplish
value++usingoperator(you will need the symbolic toolbox though).operator(symb, f, T, prio)defines a new operator symbolsymbof typeT(Prefix | Postfix | Binary | Nary) with priorityprio. The functionfevaluates expressions using the new operator.Given the operator symbol “++”, say, with evaluating function
f, the following expressions are built by the parser, depending on the type of the operator, where :Prefix: The input ++x results in f(x).
Postfix: The input x++ results in f(x).
Binary: The input x ++ y ++ z results in f(f(x, y), z).
Nary: The input x ++ y ++ z results in f(x, y, z)).
see more at matlab’s documentation.