# Find if a value greater than a threshold occurs 10 or more times in every consecutive 20 days.

13 ビュー (過去 30 日間)
SMA 2015 年 11 月 9 日
コメント済み: Image Analyst 2023 年 4 月 20 日
I am having trouble writing a code for a condition that within every 20 consecutive days, find if a value greater than a defined threshold occurs at least 10 times. For example, I have a matrix called test_matrix (57x365; where each row represents a year and each column is a day). So for each of the 57 years, I want to find the values greater than a threshold (say value >5), and check whether they occur 10 or more times in every consecutive 20 days.
If the criteria is met, I'd like to know all the first days of the 20 day window (i.e., it may occur 3 times in a year, so I'd like to know those three days).
A bit hard to explain, so I am happy to clarify the question. Thanks!
##### 3 件のコメント1 件の古いコメントを表示1 件の古いコメントを非表示
SMA 2015 年 11 月 20 日

I have not really created a code for this yet. This is a small part of the large code and I am absolutely stuck here.
SMA 2015 年 11 月 21 日
So if I have an array; X = [0 0 0 1 1 1 0 1 0 1 1 0 1 0], and I want to take sum of every 3 consecutive numbers; so the answer would be; Ans = [0 1 2 3 2 2 1 2 2 2 2 1 1 0] how can I do it and then find every time the sum is >= 3? In the above example it happens only once on the 4th day (say each column is a day).

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

### 採用された回答

Image Analyst 2015 年 11 月 21 日

First of all you need to convert the matrix into one long 1-D vector so you can find stretches than span New Year's Day. Then you simply use conv():
allDays = reshape(test_matrix', [1, numel(test_matrix)]);
% Find days more than a threshold of 5:
aboveThreshold = allDays >= 5;
% Make a moving window of 20 days to count the number of days above 5.
windowWidth = 20;
counts = conv(aboveThreshold, ones(1, windowWidth), 'same');
tenOrMore = counts >= 10;
Note that there there will be an offset so you have to see what it is. The counts is basically the counts when the window is centered at the location, but since you're not taking an odd number of elements in the window as is normal, there will be a half element shift and you'll need to figure that out.
##### 3 件のコメント1 件の古いコメントを表示1 件の古いコメントを非表示
Image Analyst 2015 年 11 月 21 日
Of course. If you don't want to count stretches that span across New Year's Day then just leave it as a 2D array and use conv2():
% Find days more than a threshold of 5:
aboveThreshold = test_matrix>= 5;
% Make a moving window of 20 days to count the number of days above 5.
windowWidth = 20;
counts = conv2(aboveThreshold, ones(1, windowWidth), 'same');
tenOrMore = counts >= 10;
SMA 2015 年 11 月 22 日
Since I needed some months in the middle, I have made it work with some modification. Thank you! This was of great help.

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

### その他の回答 (1 件)

Bala 2023 年 4 月 20 日
i need find same value repeated more than three times place in column in matlab code
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Image Analyst 2023 年 4 月 20 日
@Bala Did you try a simple for loop?
v = [1,2,3,3,3,3,4,5,5,5,6,6,6,6,6,6];
startingIndexes = nan(1, numel(v));
for k = 3 : length(v)
% See if the prior element, and the one before that match the current element.
if (v(k) == v(k-1)) && (v(k) == v(k-2))
% There are 3 adjacent elements that have the same value.
% Record the location of the starting index.
startingIndexes(k-2) = k-2;
end
end
startingIndexes
startingIndexes = 1×16
NaN NaN 3 4 NaN NaN NaN 8 NaN NaN 11 12 13 14 NaN NaN
% Or
startingIndexes = startingIndexes(~isnan(startingIndexes))
startingIndexes = 1×7
3 4 8 11 12 13 14
To learn other fundamental concepts, invest 2 hours of your time here:

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

### カテゴリ

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

### Community Treasure Hunt

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

Start Hunting!

Translated by