MATLAB: How to keep the first number in a series of positive numbers & replace all others with 0

series

Hi, I need some help with the following problem:
I have a series of positive & negative numbers which looks like this:
1 2 3 -1 -3 4 4 6 -3 -7
I would like to keep the first positive number in a series of positive numbers & set all other positive numbers in that series to 0. At the same time, I would like to keep the last negative number in a series of negative numbers. So what I need in the end is:
1 0 0 0 -3 4 0 0 0 -7
It would be great if someone could help me! Thanks, Hannah

Best Answer

diff and some logical comparisons will solve this:
>> V = [1,2,3,-1,-3,4,4,6,-3,-7]
V =
1 2 3 -1 -3 4 4 6 -3 -7
>> X = diff([-V(1),V,-V(end)]>0)<1;
>> V(X(1:end-1) & X(2:end)) = 0;
V =
1 0 0 0 -3 4 0 0 0 -7
Test it: it works.