counting how many times a certain value is repeated in a fixed interval

2 ビュー (過去 30 日間)
Finda Putri
Finda Putri 2020 年 6 月 27 日
回答済み: Finda Putri 2020 年 6 月 29 日
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
  2 件のコメント
Tommy
Tommy 2020 年 6 月 27 日
Is the score 3 because of the following three 1s?
X = [0,0,1,1,0,1,1,1,1,1,0,0]
% ^ ^ ^
% 1 +1 +1 = 3
Would this have a score of 4?
X = [0,0,1,1,0,1,1,1,1,1,1,0]
% ^ ^ ^ ^
% 1 +1 +1 +1 = 4
And this a score of 2?
X = [0,0,1,1,0,1,1,1,0,1,0,0]
% ^ ^
% 1 +1 = 2
Finda Putri
Finda Putri 2020 年 6 月 27 日
編集済み: madhan ravi 2020 年 6 月 28 日
no it does not work as pair.. I just happened to randomly assigned two-times 1's as my criteria for an example.
X = [0,0,1,1,0,1,1,1,1,1,0,0]
% ^1 ^1 ^1 ^this 1 no longer counts as 0 comes after that..

サインインしてコメントする。

回答 (4 件)

Matt J
Matt J 2020 年 6 月 28 日
編集済み: Matt J 2020 年 6 月 28 日
If you have the Image Processing Toolbox,
reg=regionprops(X,'Area');
score = sum(structfun(@(a) floor(a/2), reg))

KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 6 月 28 日
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)
  1 件のコメント
Finda Putri
Finda Putri 2020 年 6 月 29 日
I find this only work if my criteria is two 1's?

サインインしてコメントする。


madhan ravi
madhan ravi 2020 年 6 月 28 日
編集済み: madhan ravi 2020 年 6 月 28 日
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!
  1 件のコメント
Finda Putri
Finda Putri 2020 年 6 月 29 日
This works, but yes only when the criteria is two 1's as you said, for 2 repetitions.

サインインしてコメントする。


Finda Putri
Finda Putri 2020 年 6 月 29 日
I found a way to answer this already, but in rather long code. Thank you very much for helping me.

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by