MATLAB Answers

Rikke
0

Remove some numbers from an array and replace them with zero?

Asked by Rikke
on 10 Apr 2019
Latest activity Commented on by Rikke
on 10 Apr 2019
Lets say the array is like this:
A = [4 4 4 4 6 6 6 6 6 6 2 2 3 3 3 5 5 5 5 5 3 3 3];
How could I change the matrix to be like this:
B = [4 0 0 0 6 0 0 0 0 0 2 0 3 0 0 5 0 0 0 0 3 0 0];
I want to represent each new number in the array but not more than once. The numbers may occure several times as number three in this example.

  0 Comments

Sign in to comment.

1 Answer

Answer by Stephen Cobeldick on 10 Apr 2019
Edited by Stephen Cobeldick on 10 Apr 2019
 Accepted Answer

>> A = [4,4,4,4,6,6,6,6,6,6,2,2,3,3,3,5,5,5,5,5,3,3,3]
A =
4 4 4 4 6 6 6 6 6 6 2 2 3 3 3 5 5 5 5 5 3 3 3
>> B = A;
>> B([false,~diff(B)]) = 0
B =
4 0 0 0 6 0 0 0 0 0 2 0 3 0 0 5 0 0 0 0 3 0 0

  1 Comment

Perfect, it worked! Thanks!

Sign in to comment.