1

If you have the following array

a = [1 1 3 4 4 1 1 4 4 4];

I want to get this result

b = [1 3 4 1 4];
c = [2 1 2 2 3];

b is an array with unique adjacent values from a,

c is an array that counts the number of repetition

Is there a way to do this without loops?

Q_1.0
  • 183
  • 9
  • 1
    What you are asking for is [run length encoding](http://en.wikipedia.org/wiki/Run-length_encoding). This has been asked to death here on StackOverflow. The one I linked to is one of the more popular posts. If you look at Mohsen's answer, simply equate `numCode` with your input array. – rayryeng May 22 '15 at 17:27
  • Yes, Mohsen's code works as well – Q_1.0 May 22 '15 at 18:21

1 Answers1

1

One approach with diff -

b = a([true diff(a)~=0])
c = diff(find([1 diff(a)~=0 1]))
Divakar
  • 218,885
  • 19
  • 262
  • 358