Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Solution 156594

Submitted on 1 Nov 2012 by José Ramón Menzinger

Correct

71Size
Leading solution size is 11.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test
Code Input and Output
1
Pass
 
%%
x = [5 3 3 1 0 9 9 4 4 4 4 5 1 2 2];
r_correct = [1 1 2 1 1 1 2 1 2 3 4 1 1 1 2];
assert(isequal(run_length(x),r_correct))
r =
     1     1     2     1     1     1     2     1     2     0     0     1     1     1     2
r =
     1     1     2     1     1     1     2     1     2     3     0     1     1     1     2
r =
     1     1     2     1     1     1     2     1     2     3     4     1     1     1     2
2
Pass
 
%%
x = ones(1,20);
r_correct = 1:20;
assert(isequal(run_length(x),r_correct))
r =
  Columns 1 through 16
     1     2     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 17 through 20
     0     0     0     0
r =
  Columns 1 through 16
     1     2     3     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 17 through 20
     0     0     0     0
r =
  Columns 1 through 16
     1     2     3     4     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 17 through 20
     0     0     0     0
r =
  Columns 1 through 16
     1     2     3     4     5     0     0     0     0     0     0     0     0     0     0     0
  Columns 17 through 20
     0     0     0     0
r =
  Columns 1 through 16
     1     2     3     4     5     6     0     0     0     0     0     0     0     0     0     0
  Columns 17 through 20
     0     0     0     0
r =
  Columns 1 through 16
     1     2     3     4     5     6     7     0     0     0     0     0     0     0     0     0
  Columns 17 through 20
     0     0     0     0
r =
  Columns 1 through 16
     1     2     3     4     5     6     7     8     0     0     0     0     0     0     0     0
  Columns 17 through 20
     0     0     0     0
r =
  Columns 1 through 16
     1     2     3     4     5     6     7     8     9     0     0     0     0     0     0     0
  Columns 17 through 20
     0     0     0     0
r =
  Columns 1 through 16
     1     2     3     4     5     6     7     8     9    10     0     0     0     0     0     0
  Columns 17 through 20
     0     0     0     0
r =
  Columns 1 through 16
     1     2     3     4     5     6     7     8     9    10    11     0     0     0     0     0
  Columns 17 through 20
     0     0     0     0
r =
  Columns 1 through 16
     1     2     3     4     5     6     7     8     9    10    11    12     0     0     0     0
  Columns 17 through 20
     0     0     0     0
r =
  Columns 1 through 16
     1     2     3     4     5     6     7     8     9    10    11    12    13     0     0     0
  Columns 17 through 20
     0     0     0     0
r =
  Columns 1 through 16
     1     2     3     4     5     6     7     8     9    10    11    12    13    14     0     0
  Columns 17 through 20
     0     0     0     0
r =
  Columns 1 through 16
     1     2     3     4     5     6     7     8     9    10    11    12    13    14    15     0
  Columns 17 through 20
     0     0     0     0
r =
  Columns 1 through 16
     1     2     3     4     5     6     7     8     9    10    11    12    13    14    15    16
  Columns 17 through 20
     0     0     0     0
r =
  Columns 1 through 16
     1     2     3     4     5     6     7     8     9    10    11    12    13    14    15    16
  Columns 17 through 20
    17     0     0     0
r =
  Columns 1 through 16
     1     2     3     4     5     6     7     8     9    10    11    12    13    14    15    16
  Columns 17 through 20
    17    18     0     0
r =
  Columns 1 through 16
     1     2     3     4     5     6     7     8     9    10    11    12    13    14    15    16
  Columns 17 through 20
    17    18    19     0
r =
  Columns 1 through 16
     1     2     3     4     5     6     7     8     9    10    11    12    13    14    15    16
  Columns 17 through 20
    17    18    19    20
3
Pass
 
%%
x = [1 1 1 2 2 3 4 4 5 5 5];
r_correct = [1 2 3 1 2 1 1 2 1 2 3];
assert(isequal(run_length(x),r_correct))
r =
     1     2     0     1     2     1     1     2     1     2     0
r =
     1     2     3     1     2     1     1     2     1     2     3
4
Pass
 
%%
x = 1:40;
r_correct = ones(size(x));
assert(isequal(run_length(x),r_correct))

                    
5
Pass
 
%%
x = [-34 -17*ones(1,100)];
r_correct = [1 1:100];
assert(isequal(run_length(x),r_correct))
r =
  Columns 1 through 16
     1     1     2     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 17 through 32
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 33 through 48
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 49 through 64
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 65 through 80
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 81 through 96
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 97 through 101
     0     0     0     0     0
r =
  Columns 1 through 16
     1     1     2     3     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 17 through 32
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 33 through 48
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 49 through 64
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 65 through 80
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 81 through 96
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 97 through 101
     0     0     0     0     0
r =
  Columns 1 through 16
     1     1     2     3     4     0     0     0     0     0     0     0     0     0     0     0
  Columns 17 through 32
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 33 through 48
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 49 through 64
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 65 through 80
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 81 through 96
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 97 through 101
     0     0     0     0     0
r =
  Columns 1 through 16
     1     1     2     3     4     5     0     0     0     0     0     0     0     0     0     0
  Columns 17 through 32
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 33 through 48
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 49 through 64
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 65 through 80
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 81 through 96
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 97 through 101
     0     0     0     0     0
r =
  Columns 1 through 16
     1     1     2     3     4     5     6     0     0     0     0     0     0     0     0     0
  Columns 17 through 32
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 33 through 48
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 49 through 64
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 65 through 80
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 81 through 96
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 97 through 101
     0     0     0     0     0
r =
  Columns 1 through 16
     1     1     2     3     4     5     6     7     0     0     0     0     0     0     0     0
  Columns 17 through 32
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 33 through 48
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 49 through 64
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 65 through 80
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 81 through 96
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 97 through 101
     0     0     0     0     0
r =
  Columns 1 through 16
     1     1     2     3     4     5     6     7     8     0     0     0     0     0     0     0
  Columns 17 through 32
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 33 through 48
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 49 through 64
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 65 through 80
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 81 through 96
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 97 through 101
     0     0     0     0     0
r =
  Columns 1 through 16
     1     1     2     3     4     5     6     7     8     9     0     0     0     0     0     0
  Columns 17 through 32
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 33 through 48
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 49 through 64
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 65 through 80
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 81 through 96
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 97 through 101
     0     0     0     0     0
r =
  Columns 1 through 16
     1     1     2     3     4     5     6     7     8     9    10     0     0     0     0     0
  Columns 17 through 32
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 33 through 48
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 49 through 64
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 65 through 80
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 81 through 96
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 97 through 101
     0     0     0     0     0
r =
  Columns 1 through 16
     1     1     2     3     4     5     6     7     8     9    10    11     0     0     0     0
  Columns 17 through 32
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 33 through 48
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 49 through 64
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 65 through 80
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 81 through 96
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 97 through 101
     0     0     0     0     0
r =
  Columns 1 through 16
     1     1     2     3     4     5     6     7     8     9    10    11    12     0     0     0
  Columns 17 through 32
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 33 through 48
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 49 through 64
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 65 through 80
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 81 through 96
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 97 through 101
     0     0     0     0     0
r =
  Columns 1 through 16
     1     1     2     3     4     5     6     7     8     9    10    11    12    13     0     0
  Columns 17 through 32
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 33 through 48
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 49 through 64
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 65 through 80
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 81 through 96
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 97 through 101
     0     0     0     0     0
r =
  Columns 1 through 16
     1     1     2     3     4     5     6     7     8     9    10    11    12    13    14     0
  Columns 17 through 32
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 33 through 48
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 49 through 64
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
  Columns 65 through 80
     0     0     0     0     0     0     0     0     0     0     0  ...