Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A minor refactoring will save the second find in instances where you don't find results, and preallocating with AverageValue with Value saves the else part.</p> <pre><code>UnwrappedRadians = unwrap( Angle ./ 180 * pi); AverageValue = Value; for i=1:length(UnwrappedRadians) mid = UnwrappedRadians(i); start = find( UnwrappedRadians(1:i) &lt; (mid - pi/2), 1, 'Last'); if ~isempty(start) finish = find( UnwrappedRadians(i:end) &gt; (mid + pi/2), 1, 'First'); if ~isempty(finish) AverageValue(i) = mean(Value(start:finish+i-1)); % nanmean end end end </code></pre> <p>If you find that the calculation of finish is empty more often than the calculation of start, you can switch their order so that the finish check is done first.</p> <p>It's not clear if UnwrappedRadians will always be sorted. If so, you can reuse the results from earlier finds to reduce the size of the subvector you search over. For instance, if the you look for the last 1 between 11 and the end of the vector and it is element 23, when you search for 1.1, you can reduce the search to between 24 and the end of the vector. I've found that this technique can yield really big increases in speed.</p> <p>Vectorization is difficult in cases like this, because you are using the indexing variable (i) as an index again (in the find statements). It is possible to rig something up using arrayfun, but it is highly unlikely to be faster (I would guess slower, actually) and will definitely be less readable than what you have.</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