jarhead
jarhead

Reputation: 1901

Pad consecutive repeating elements with zeros

Consider the following vector:

A=[1 1 1 2 2 2 1 1 1 1 3 2 2 4 4 4 4]

How can any consecutive values can be padded such that the result vector will be given by:

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

Consider cases where the first element is non-zero.

Upvotes: 0

Views: 43

Answers (1)

Cris Luengo
Cris Luengo

Reputation: 60564

Based on this other answer, which removes consecutive repeating elements, we can build an answer to this question.

A = [ 1 1 1 2 2 2 1 1 1 1 3 2 2 4 4 4 4];
I = [false,diff(A)==0];
B = A; B(I) = 0;

Upvotes: 5

Related Questions