 <p><strong>Approach #1: Generic case</strong></p> <p>One approach with <a href="http://in.mathworks.com/help/matlab/ref/bsxfun.html" rel="nofollow"><code>bsxfun</code></a> and <code>matrix-multiplication</code> -</p> <pre><code>mask = bsxfun(@ge,indx_pos,event_start.') &amp; bsxfun(@le,indx_pos,event_end.') Etotal = energy.'*mask </code></pre> <p>This could be a bit <code>memory-hungry</code> if <code>indx_pos</code> has lots of elements in it.</p> <hr> <p><strong>Approach #2: Non-overlapping start/end ranges case</strong></p> <p>One can use <a href="http://in.mathworks.com/help/matlab/ref/accumarray.html" rel="nofollow"><code>accumarray</code></a> for this special case like so -</p> <pre><code>%// Setup ID array for use in accumarray later on loc(numel(pos))=0; %// Fast pre-allocation scheme valids = event_end+1&lt;=numel(pos); loc(event_end(valids)+1) = -1*(1:sum(valids)); loc(event_start) = loc(event_start)+(1:numel(event_end)); id = cumsum(loc); %// Set elements as zeros in HitEnergy that do not satisfy the criteria: %// pos&gt;0.7 &amp; pos&lt;2.0 HitEnergy_select = (pos&gt;0.7 &amp; pos&lt;2.0).*HitEnergy(:); %// Discard elments in HitEnergy_select &amp; id that have IDs as zeros HitEnergy_select = HitEnergy_select(id~=0); id = id(id~=0); %// Accumulate summations as done inside the loop in the original code Etotal = accumarray(id(:),HitEnergy_select); </code></pre>