MATLAB: Negative continuous position on vector

findlengthMATLABunique

Hi I have a question, i know how to solve with the code I show in the bottom of the page but i think that it has to be an easyer way. What I want to do is: starting with a vector row AA containing only one one in one position, for example:
AA = [0 0 1 0 0 0 0];
I want to get a BB vector that's next:
BB = [-2 -1 0 1 2 3 4];
A vector in which the numbers to the left of 1 are negative crescent in which the first component is the most negative position possible, the point 1 becomes 0 and the right of 1 is positive crescent until the end of the vector.
The code I know how to do is this:
AA = [0 0 1 0 0 0 0]
[m,n] = size(AA);
x = find(AA(1,:),1)
neg = AA(1:x-1)
BB(1,x) = 0
BB(1,1:length(neg)) = -length(neg):1:-1
BB(1,x+1:n) = 1:1:n-x
But I don't see that this is a very good way to get the solution…

Best Answer

BB = (1:length(AA))-find(AA==1,1)