Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>MatlabSorter has provided a refactoring which makes sense to me, so if <em>your code</em> really does what you want, his refactoring is the way forward :-). Note that in my tests with numel(Angle)=50000, his refactoring did not save much (probably because my sample data assumed that the find()s will almost never fail except at the very beginning and at the end of your data trace).</p> <p>However, while looking at your code, I was wondering: Are you sure you absolutely want to average all values from the first time the angle gets into the mid-pi/2...mid+pi/2 range, until the last time it leaves that range? If your unwrapped Angles are non-monotonous (for example if backwards movements are allowed, if the sampling rate is too low to avoid aliasing, or simply due to measurement noise in the angle) then you will also be averaging over some values <em>outside</em> (and possibly well outside) the 180° range. </p> <p>Note that in any case the first measurement (Value(start)) you average over is <em>always</em> more than pi/2 before your "mid" angle (you start with the last angle before the interval), while your last measurement (Value(finish+i-1)) is <em>always more</em> than pi/2 behind mid. So your effective range you average over is always larger than pi, even if data values at exactly mid-pi/2 and mid+pi/2 are available... is that really intended?</p> <p>So in case you are really interested in averaging only Values where Angle is less than pi/2 from mid, here is my code suggestion, which sadly has only marginally quicker runtime than what you currently use. Note that this is NOT a refactoring, because it acts differently from your code in the two ways described above.</p> <pre><code>UnwrappedRadians = unwrap( Angle ./ 180 * pi); AverageValue = Value; avgstart=find( UnwrappedRadians &gt; (UnwrappedRadians(1) + pi/2), 2, 'First'); avgend=find( UnwrappedRadians &lt; (UnwrappedRadians(end) - pi/2), 1, 'Last'); for i=avgstart:avgend AverageValue(i) = mean(Value(abs(UnwrappedRadians-UnwrappedRadians(i)) &lt;= pi/2)); % nanmean end </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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.
 

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