special sorting

조회 수: 7 (최근 30일)
Hamed Amini
Hamed Amini 2011년 8월 15일
special sorting! i have the input vector of flags for different segments of the vector (flags are distributed randomly). how can i sort it to get the output? basically it reads from the beginning of the vector and labels each segment of the vector consecutively. there is no repetitive flags in the input vector for different segments.
input = [2 2, 5, 6 6 6, 1 1 1 1, 4, 3 3]
output= [1 1, 2, 3 3 3, 4 4 4 4, 5, 6 6]
  댓글 수: 2
Paulo Silva
Paulo Silva 2011년 8월 15일
Sorry but I can't understand, you show the flags (input) and the output but no original vector to be sorted
Hamed Amini
Hamed Amini 2011년 8월 15일
Sorry, I think I was not clear enough. What I was looking for was what Jan did below.

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2011년 8월 15일
Do not use "input" as name of a variable, because it is a name of a built-in function.
in = [2, 2, 5, 6, 6, 6, 1, 1, 1, 1, 4, 3, 3];
out = cumsum([1, diff(a) ~= 0]);
>> out = [1 1, 2, 3, 3, 3, 4, 4, 4, 4, 5, 6, 6]
  댓글 수: 2
Hamed Amini
Hamed Amini 2011년 8월 15일
Thanks for the clever solution and the hint about the variable names.
Paulo Silva
Paulo Silva 2011년 8월 15일
Now I see what was the objective, +1 vote

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by