Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Given the information that <code>Polygon</code> is 2 dimensional, <code>point</code> is a row vector and the other variables are scalars, here is the first version of your new function (scroll down to see that there are lots of ways to skin this cat):</p> <pre><code>function [result] = newHitTest (point,Polygon,r,tol,stepSize) result = 0; linDiff = Polygon-repmat(point,size(Polygon,1),1); testLogicals = sqrt( sum( ( linDiff ).^2 ,2 )) &lt; tol*r; if any(testLogicals); result = circleTest (point,Polygon,r,tol,stepSize); end </code></pre> <p>The thought process for vectorization in Matlab involves trying to operate on as much data as possible using a single command. Most of the basic builtin Matlab functions operate very efficiently on multi-dimensional data. Using <code>for</code> loop is the reverse of this, as you are breaking your data down into smaller segments for processing, each of which must be interpreted individually. By resorting to data decomposition using <code>for</code> loops, you potentially loose some of the massive performance benefits associated with the highly optimised code behind the Matlab builtin functions. </p> <p>The first thing to think about in your example is the conditional break in your main loop. You cannot break from a vectorized process. Instead, calculate all possibilities, make an array of the outcome for each row of your data, then use the <code>any</code> keyword to see if any of your rows have signalled that the <code>circleTest</code> function should be called. </p> <p>NOTE: It is not easy to efficiently conditionally break out of a calculation in Matlab. However, as you are just computing a form of Euclidean distance in the loop, you'll probably see a performance boost by using the vectorized version and calculating all possibilities. If the computation in your loop were more expensive, the input data were large, and you wanted to break out as soon as you hit a certain condition, then a matlab extension made with a compiled language could potentially be much faster than a vectorized version where you might be performing needless calculation. However this is assuming that you know how to program code that matches the performance of the Matlab builtins in a language that compiles to native code.</p> <p>Back on topic ... </p> <p>The first thing to do is to take the linear difference (<code>linDiff</code> in the code example) between <code>Polygon</code> and your row vector <code>point</code>. To do this in a vectorized manner, the dimensions of the 2 variables must be identical. One way to achieve this is to use <code>repmat</code> to copy each row of <code>point</code> to make it the same size as <code>Polygon</code>. However, <code>bsxfun</code> is usually a superior alternative to repmat (<a href="https://stackoverflow.com/questions/12951453/in-matlab-when-is-it-optimal-to-use-bsxfun/12955706#comment17565043_12955706">as described in this recent SO question</a>), making the code ...</p> <pre><code>function [result] = newHitTest (point,Polygon,r,tol,stepSize) result = 0; linDiff = bsxfun(@minus, Polygon, point); testLogicals = sqrt( sum( ( linDiff ).^2 ,2 )) &lt; tol*r; if any(testLogicals); result = circleTest (point,Polygon,r,tol,stepSize); end </code></pre> <p>I rolled your <code>d</code> value into a column of <code>d</code> by summing across the 2nd axis (note the removal of the array index from <code>Polygon</code> and the addition of <code>,2</code> in the <code>sum</code> command). I then went further and evaluated the logical array <code>testLogicals</code> inline with the calculation of the distance measure. You will quickly see that a downside of heavy vectorisation is that it can make the code less readable to those not familiar with Matlab, but the performance gains are worth it. Comments are pretty necessary.</p> <p>Now, if you want to go completely crazy, you could argue that the test function is so simple now that it warrants use of an 'anonymous function' or 'lambda' rather than a complete function definition. The test for whether or not it is worth doing the <code>circleTest</code> does not require the <code>stepSize</code> argument either, which is another reason for perhaps using an anonymous function. You can roll your test into an anonymous function and then jut use <code>circleTest</code> in your calling script, making the code self documenting to some extent . . . </p> <pre><code>doCircleTest = @(point,Polygon,r,tol) any(sqrt( sum( bsxfun(@minus, Polygon, point).^2, 2 )) &lt; tol*r); if doCircleTest(point,Polygon,r,tol) result = circleTest (point,Polygon,r,tol,stepSize); else result = 0; end </code></pre> <p>Now everything is vectorised, the use of function handles gives me another idea . . . </p> <p>If you plan on performing this at multiple points in the code, the repetition of the <code>if</code> statements would get a bit ugly. To stay <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself" rel="nofollow noreferrer">dry</a>, it seems sensible to put the test with the conditional function into a single function, just as you did in your original post. However, the utility of that function would be very narrow - it would only test if the <code>circleTest</code> function should be executed, and then execute it if needs be. </p> <p>Now imagine that after a while, you have some other conditional functions, just like <code>circleTest</code>, with their own equivalent of <code>doCircleTest</code>. It would be nice to reuse the conditional switching code maybe. For this, make a function like your original that takes a default value, the boolean result of the computationally cheap test function, and the function handle of the expensive conditional function with its associated arguments ...</p> <pre><code>function result = conditionalFun( default, cheapFunResult, expensiveFun, varargin ) if cheapFunResult result = expensiveFun(varargin{:}); else result = default; end end %//of function </code></pre> <p>You could call this function from your main script with the following . . . </p> <pre><code>result = conditionalFun(0, doCircleTest(point,Polygon,r,tol), @circleTest, point,Polygon,r,tol,stepSize); </code></pre> <p>...and the beauty of it is you can use any test, default value, and expensive function. Perhaps a little overkill for this simple example, but it is where my mind wandered when I brought up the idea of using function handles.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload