sort function
5 次查看(过去 30 天)
显示 更早的评论
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!!
0 个评论
采纳的回答
Matt Fig
2011-5-2
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...
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!