MATLAB: Finding the increase in values

increasing values

suppose i have a column matrix a=( 2 2 2 3 3 3 5 5 5 4 4 3 3 3 2 2 3 3 3 4 4 4 5 5 5 4 4 3 3 )
i want to find the constant and increasing elements and put them separately in different columns of a matrix. eliminating the decreasing elements.
output(:,1) = ( 2 2 2 3 3 3 5 5 5 )
output(:,2) = (2 2 3 3 3 4 4 4 5 5 5 )

Best Answer

a = [2;2;2;3;3;3;5;5;5;4;4;3;3;3;2;2;3;3;3;4;4;4;5;5;5;4;4;3;3]
idx = [true;diff(a)~=0];
idy = diff(a(idx))>0;
idd = diff([0;idy;0]);
idb = find(idd>0);
ide = find(idd<0);
ids = cumsum(idx);
fun = @(b,e)a(ismember(ids,b:e));
C = arrayfun(fun,idb,ide,'uni',0);
And checking the output:
>> C{:}
ans =
2
2
2
3
3
3
5
5
5
ans =
2
2
3
3
3
4
4
4
5
5
5