MATLAB Answers

0

How do I count the number of new occurrences of a number in an array?

Asked by Tyron Louw on 24 Jul 2018
Latest activity Answered by Paolo
on 24 Jul 2018
I have an array given as
x = [1 1 1 1 1 1 1 2 2 1 1 1 3 3 3 3 1 1 1 1 3 1 5]
I am trying to calculate how many new occurrences there are of the number 1. Importantly, this should ignore whether or how many times 1 recurs in the adjacent cell. In the above example, the answer would be 4, because there are 4 instances of 1 appearing, even though they differ in length.
Thanks!

  0 Comments

Sign in to comment.

Tags

Products


Release

R2017a

1 Answer

Answer by Paolo
on 24 Jul 2018

nnz(diff([0 (x==1)])==1)

  0 Comments

Sign in to comment.