0

I've a vector

A = [0;1;1;1;0;0;1;1;1;2;2;2;2];

and I want to count the number of equal adjacent values in order to have a matrix like this:

B=[0 1
   1 3
   0 2
   1 3
   2 4];

can you help me?

gabboshow
  • 5,359
  • 12
  • 48
  • 98

1 Answers1

1

Here is a short solution:

J=find(diff([A(1)-1; A]));
B=[A(J), diff([J; numel(A)+1])];
Mohsen Nosratinia
  • 9,844
  • 1
  • 27
  • 52