5 views (last 30 days)

Hi,

I need a help for a relatively simple calculation, but I keep failing to solve it.

Say I have the following data, where keeping the order is necessary:

X = [0,0,1,1,0,1,1,1,1,1,0,0]

and I want to know how many times '1' is repeated for 2x, and that two '1's is considered as 1 score. So score for X should be 3.

I have a relatively long data (1x74240) with 0, 1, and -1.

With the logic above, I need to know the score for when the data shows repeated 1 and repeated -1.

Any sort of input is appriciated.

Thanks

KALYAN ACHARJYA
on 28 Jun 2020

Multiple Ways: One way

X=[0,0,1,1,0,1,1,1,1,1,0,0];

idx=find(X==1)

for i=1:2:length(idx)-1

data(i)=idx(i+1)-idx(i)

end

score=sum(data)

madhan ravi
on 28 Jun 2020

Edited: madhan ravi
on 28 Jun 2020

According to your comment

No toolboxes/loops needed:

z = (X(1:2:end)==1) + (X(2:2:end)==1) % works for even number of elements only!

score = nnz(z==2) % works only for 2 repetitions!

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/555889-counting-how-many-times-a-certain-value-is-repeated-in-a-fixed-interval#comment_915919

⋮## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/555889-counting-how-many-times-a-certain-value-is-repeated-in-a-fixed-interval#comment_915919

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/555889-counting-how-many-times-a-certain-value-is-repeated-in-a-fixed-interval#comment_915925

⋮## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/555889-counting-how-many-times-a-certain-value-is-repeated-in-a-fixed-interval#comment_915925

Sign in to comment.