i want to check, if there are values of ascending order within a vector.
eg.:
vector:
[... 10 1 2 3 4 5 56 ...]
the function should check, if there are eg. 5 values in ascending order within this vector,
here the function should recognise, that [1 2 3 4 5] is in my vector and should tell me, yes, there are 5 values in ascending order and where (index).
but it should also recognise eg.
[... 100 50 51 52 53 54 20 ...]
here the function should recognise, that there are 5 values in ascending order, from 50 to 54.
my first idea was, to make a loop, that looks like this:
for i=1:n
if vector((1:5)+i)-vector(i)==[0 1 2 3 4]
'there are five values in ascending order starting at index i'
end
end
this would work, but has anybody an idea how this could work without having a loop? and faster? my priority is, that the function should be FAST!
any help is appreciated very much!
\Roland
you might want to try following:
X = [1 2 3 4 3 2 1 2 3 4 5];
Use the diff function to find if you have increasing or decreasing values.
dX = [0 diff(X)]
dX =
1 1 1 -1 -1 -1 1 1 1 1
Create a logical vector for values of dX greater than zero
I = dX>0
I =
1 1 1 0 0 0 1 1 1 1
Then use the function accumconncomps on the FEX
http://www.mathworks.com/matlabcentral/fileexchange/21397
to count the number of subsequent equal numbers in I
[a,b] = accumconncomps(I)
a =
1 0 1
b =
3 3 4
a contains the values in the logical array (1 for sections with increasing values, 0 for decreasing or equal values). b contains the number of elements in each section. Note that the dX vector contains one element less than the original vector. Moreover, you should also check for equal subsequent values.
Best regards,
Wolfgang
"Roland " <burg...@gmx.de> wrote in message <h0m1hc$3uh$1...@fred.mathworks.com>...
dX = [0 diff(X)] should be dX = diff(X) in the above example.
Best, Wolfgang
"Wolfgang Schwanghart" <schwanghart...@googlemail.com> wrote in message <h0m3pu$5pe$1...@fred.mathworks.com>...
No need for something fancy. Use STRFIND on the logical differences:
% your data
V = [10 30 10 100 50 51 52 53 54 20 22 13 45 80 13]
N = 5 ;
% engine
d = diff(V)>0
i0 = strfind([0 d],[0 1]) % where do runs begin
i1 = strfind([d 0],[1 0])+1 % where do runs end
L = i1 - i0 + 1 % length of each run
startidx = i0(L==N)
% check % first index, in case of multiple occurences
V(startidx(1):startidx(1)+N-1)
hth
Jos
http://www.mathworks.com/matlabcentral/fileexchange/24255
>
> my first idea was, to make a loop, that looks like this:
> for i=1:n
> if vector((1:5)+i)-vector(i)==[0 1 2 3 4]
> 'there are five values in ascending order starting at index i'
> end
> end
>
> this would work, but has anybody an idea how this could work without having a loop? and faster? my priority is, that the function should be FAST!
>
> any help is appreciated very much!
>
> \Roland
Why reinvente the wheel?
http://www.mathworks.com/matlabcentral/fileexchange/24255
[c loc]=SplitVec(a, 'consecutive', 'split', 'loc');
c{:}
loc{:}
% Bruno
% data;
x = randn(1,100);
x(11:15) = 1:5;
x(51:55) = 11:15;
% engine;
strfind(diff(x), [1 1 1 1])
If you want to be precise about 5 consequtive numbers:
strfind(diff(x) == 1,[0 1 1 1 1 0])+1
\Roland
Does it have to be a string exactly 5 numbers long? For example, in
[100 1 2 3 4 5 6 7 99]
would your code identify all three sequences
[1 2 3 4 5]
[2 3 4 5 6]
[ 3 4 5 6 7]
If so, the following would work
Indices=strfind( diff(vector), [1 1 1 1] );
but it's easily extendable to the "5 long only" scenario.