MATLAB: Sort function

sort

Hi everybody,
I have a problem and I'll be thankful if you can help me.
I've used "sort" function and I have sorted vector and also indices of sorted elements. I want to check if the sorted elements are in a row or not?!
I want to know if there is a better way than for loop and check it one by one!!

Best Answer

I think you will have to clarify your question more. What do you mean by "in a row?" Does this mean that they are integers separated by 1? What if there are repeats, does that count as "in a row?" Do you mean you want to know whether the elements in the sorted array are "in a row" or the elements in the original array?
%
%
%
EDIT In response to your clarification.
Look at using the DIFF function to produce a logical index:
A = [3 14 7 4 1 8 12 5]
[sorted,indices]=sort(A,'descend')
idx = [0 diff(indices)];
idx = abs(idx)==1
slct = indices(find(~idx,4)) % The first four non-consecutive indices.
Of course it could be that once this is done, the removal of an element of the indices vector could cause another set of consecutive elements to appear in the slct vector... If you really want to avoid this possibility, put the above in a WHILE loop, or just do the whole thing with a FOR loop to begin with...