0

first of all take my appoligy for my bad english.

I am trying to optimize par of my code using cellfun. (Now I know cellfun is not going to optimize perfomance, just shorten the code).

l = [1,20,10,1];
for k=1:length(edg)
    e = edg{k};
    d = lineSegmentIntersect(l, e);
    d2 = lineSegmentIntersect([l(3) l(4) l(1) l(2)], e);
    if d.intAdjacencyMatrix
        if d.intMatrixX == e(1) && d.intMatrixY == e(2) ||  d.intMatrixX == e(3) && d.intMatrixY == e(4)
        else
            it = 1; 
            break;
        end
    elseif d2.intAdjacencyMatrix  
        if d2.intMatrixX == e(1) && d2.intMatrixY == e(2) ||  d2.intMatrixX == e(3) && d2.intMatrixY == e(4)
        else
            it = 1;
            break;
        end
    end
end

I mannaged to calc lineSegmentIntersect for each l,edg pair,

d = cellfun(@(x) lineSegmentIntersect(x,l),edg);
d2 = cellfun(@(x) lineSegmentIntersect(x,[l(3) l(4) l(1) l(2)]),edg);

but I cannot find any information on how to use conditions inside the cellfun. Is it even possible? (this is not relevant anymore)

If not, is there any other way of optimizing this?

Function lineSegmentIntersect returns a struct, from which I am using 3 properties for my conditions:

  • intAdjencyMatrix (if 1 the segments intersects)
  • intMatrixX (x coordinate of the intersection)
  • intMatrixY (y coordinate of the intersection)

If l intersects one of the edg, than I want to return 1.

Would be realy greatful for any hints.

UPDATE: I tried to use vectorization but I cannot find a similar case, or this one is just to complex for vectorization?.

Thanks alot!

  • What conditions are you trying to use? Could you also post your function `lineSegmentIntersect`? – Tim Adams Feb 11 '16 at 18:15
  • 1
    [arrayfun and cellfun is not faster than a for loop](http://stackoverflow.com/questions/12522888/arrayfun-can-be-significantly-slower-than-an-explicit-loop-in-matlab-why), it just results in shorter code. – Daniel Feb 11 '16 at 18:15
  • @Daniel thank you for sharing that, I realy need to optimize my performance. Do you have any suggestion on how to? Thanks! – bonbonvoyage Feb 11 '16 at 18:18
  • @Tim Adams the conditions are visible in the code above the cellfun, the if else segment to be precise. I want to use them with the result of lineSegmentIntersect function – bonbonvoyage Feb 11 '16 at 18:20
  • @bonbonvoyage: Without being able to run the code and run it, I can't give any advice. – Daniel Feb 11 '16 at 18:22
  • @Daniel You can find lineSegmentIntersect code [here](http://www.mathworks.com/matlabcentral/fileexchange/27205-fast-line-segment-intersection/content/lineSegmentIntersect.m). Example of edg: edg = {[1,10,11,2],[1,50,11,2],[1,10,21,2]} – bonbonvoyage Feb 11 '16 at 18:27

0 Answers0