2
votes

I would like to know if there is an easy way to do the following in Matlab. Let say that I have a vector:

>> DataVector = [ 1 2 3 3 4 4 4 4 5 6 6 ]

I would like to find a function that, for each repeated element, returns the first and last index of the sequence. So for DataVector I would like something as:

>> Intervals = ComputeIntervals(DataVector)
Intervals =
 3  4
 5  8
10 11

For a simpler case where each element is repeated no more than two times i found this solution

>> DataVector = [ 1 2 3 3 4 4 5 6 6 ]
Intervals(:,1) = find(diff(DataVector) == 0)
Intervals(:,2) = find(diff(DataVector) == 0) + 1

But when an element is repeated three or more times as in the general case I have not found an easy way to generalize. Thanks in advance.

1

1 Answers

4
votes

Adapting from this answer to a similar question:

DataVector = [ 1 2 3 3 4 4 4 4 5 6 6 ];
DataVector = DataVector(:); %// make column vector
ind =  find([1; diff(DataVector); 1]); %// index of each element that differs from the next
result = [ind(1:end-1) ind(2:end)-1]; %// starts and ends of runs of equal values
result = result(diff(result,[],2)~=0,:) %// keep only runs of length greater than 1

If, as in your example, the values can only repeat in a single run (so [1 1 2 2 2 3 3 4] is allowed but [1 1 2 2 2 1 1 4] is not), the following approach using unique is also possible:

[~, starts] = unique(DataVector(:),'first'); %// first occurrence of each value
[~, ends] = unique(DataVector(:),'last');  %// last occurrence of each value
result = [starts ends];
result = result(diff(result,[],2)~=0,:);  %// keep only runs of length greater than 1