Cuda Thrust get previous element in a segment
<p>I've a vector of values and a vector of keys(indicating a segment)</p>

<pre><code>v = [1, 1, 1, 2, 3, 5, 6]
k = [0, 0, 1, 1, 0, 2, 2]
</code></pre>

<p>For each element I want to know its previous element(in the same segment).
It could be value or an index in the original vector doesn't matter.</p>

<p>So result should be(in case of value)</p>

<pre><code>r = [nan, 1, nan, 1, 1, nan, 5]
</code></pre>

<p>you can use any element instead of <code>nan</code>, for remaining part of an algorithm no matters.</p>

<p>Probably I can archive it with exclusive segmented scan and <code>max</code> operation instead of <code>sum</code>.
So two questions:</p>

<ol>
<li>Is my approach correct?</li>
<li>Is any more elegant or efficient solution? </li>
</ol>