2

JavaやC++などで可能であることは知っていますが、Matlabでも可能ですか?最近、Matlabには、value++代わりに使用する必要があるようなショートカットがないことを発見しvalue = value+1たので、この関数を反復関数に変換できるかどうか疑問に思っています。どこから始めたらいいのかわからない。もしそうなら、それは再帰関数よりも有益ではありませんか?

    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;

基本的に、私は現在、この2番目の関数を再帰的に呼び出す関数を持っています。この再帰関数を、コマンドの反復セットとして呼び出す関数に統合したいと思っていました。遅くなると確信していますが、速度は私にとって問題ではなく、ループがどのように機能するかを確認したいと思います。ありがとう。

4

1 に答える 1

1

You can accomplish value++ using operator (you will need the symbolic toolbox though).

operator(symb, f, T, prio) defines a new operator symbol symb of type T (Prefix | Postfix | Binary | Nary) with priority prio. The function f evaluates 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.

于 2013-02-14T22:33:53.590 に答える