Code for Counting and Lookup
1 回表示 (過去 30 日間)
古いコメントを表示
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/370756/image.png)
I have imported a csv file into MATLAB. The number of rows in the table is 81 so there is a long series of '1's and '0's in the single column. I want to check that whenever the consecutive number of rows with '1's is greater than 4, then flag it and note the time stamp corresponding to the first '1' for that consecutive rows of '1'. Do this for every series of 1's appearing successively for more than 4 times i.e obtain the corresponding time. The time stamps are given in the column Var1.
4 件のコメント
Rik
2020 年 10 月 4 日
Did you mean elseif?
You forgot to format the code, and you forgot to account for the length of your variable. What happens when i is equal to the length of the table?
採用された回答
Adam Danz
2020 年 10 月 4 日
編集済み: Adam Danz
2020 年 10 月 4 日
Here's a demo.
v is the input vector of 1s and 0s (or Trues and Falses)
r is the output vector of row numbers in v that start 4+ consecutive 1s.
% Demo data: v is a vec of 1s and 0s (or Trues and Falses)
A = [1 0 0 1 1 1 1 1 1 0 0 1 1 1 0 1 0 0 0 1 1 1 1 0 1 1 1 1 1 0 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 1 1 1 1 1]';
% Length of each group of consecutive 1s
B = diff(find([0;A(:);0]==0))-1;
B(B==0) = [];
% Index of 1st '1' in each group of consecutive 1s
firstIdx = find(diff([0;A(:)])==1);
% Row number of the first 1 in groups of 4 or more consecutive 1s
minConsec = 4;
r = firstIdx(B >= minConsec);
Result:
r =
4
20
25
42
48
14 件のコメント
Adam Danz
2021 年 2 月 19 日
>I need help in counting the consecutive number of 1's.
Look at variable B in my answer. A comment in my answer describes B as "Length of each group of consecutive 1s".
その他の回答 (1 件)
Image Analyst
2020 年 10 月 17 日
If you have the Image Processing Toolbox, you can also use bwareaopen() and regionprops():
A = [1 0 0 1 1 1 1 1 1 0 0 1 1 1 0 1 0 0 0 1 1 1 1 0 1 1 1 1 1 0 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 1 1 1 1 1]';
A = bwareaopen(logical(A), 4); % Just extract runs of 4 or longer.
props = regionprops(A, 'PixelIdxList') % Find indexes of all runs of 1's.
% Done! Results are in the table.
% Print out all runs where the length is 4 or more
startingIndexes = zeros(height(props), 1);
for k = 1 : height(props)
startingIndexes(k) = props(k).PixelIdxList(1);
fprintf('Region %d starts at index %d.\n', ...
k, startingIndexes(k));
end
Gives the same results as Adam's solution.
Region 1 starts at index 4.
Region 2 starts at index 20.
Region 3 starts at index 25.
Region 4 starts at index 42.
Region 5 starts at index 48.
If you also want the lengths of the runs in addition to their starting index, just ask regionprops() for 'Area':
props = regionprops(A, 'PixelIdxList', 'Area') % Find lengths and indexes of all runs of 1's.
allLengths = [props.Area]
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Help and Support についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!