# Sum values on the maximum number of consecutive days

2 ビュー (過去 30 日間)
Eli 2023 年 6 月 26 日

Dear all,
I want to accomplish the following:
1. I have a variable (R_3) with the daily data for 1 year (matrix size is 366 rows, 1 column).
2. Implement a condition of daily data > 1.
3. For the days that satisfy (2), get the maximum number of consecutive days and the corresponding row number.
4. Sum the values according to the row number.
5. In R_3, the maximum number of consecutive days is 6 days and this would be rows 106 to 111. I want to sum the values in rows 106 to 111.
I have attached my code below. I have trouble with steps 3 & 4 and do not know how to proceed.
clear; clc;
a1 = find(R_3 > 1); % Condition R_3 > 1
a2 = diff(a1);
a3 = diff([0; find(diff(a2)); numel(a2)]);
a4 = max(a3)+1; % Max number of consecutive days

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

### 回答 (2 件)

KSSV 2023 年 6 月 26 日
R_3 = R_3' ;
n = 1:length(R_3) ;
ii = zeros(size(R_3));
jj = R_3 > 1 ;
ii(strfind([0,jj(:)'],[0 1])) = 1;
idx = cumsum(ii).*jj;
out = accumarray( idx(jj)',R_3(jj)',[],@(x){x'}); % gives the values seperated in cell
out_ind = accumarray( idx(jj)',n(jj)',[],@(x){x'}); % gives the indices seperated in cell
##### 0 件のコメント-2 件の古いコメントを表示-2 件の古いコメントを非表示

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

Image Analyst 2023 年 6 月 26 日
Try this (requires the Image Processing Toolbox);
% I have a variable (R_3) with the daily data for 1 year (matrix size is 366 rows, 1 column).
% Implement a condition of daily data > 1.
a1 = R_3 > 1; % Condition R_3 > 1
% For the days that satisfy (2), get the maximum number of consecutive days and the corresponding row number.
props = regionprops(a1, 'Area', 'PixelIdxList')
props = 42×1 struct array with fields:
Area PixelIdxList
allLengths = [props.Area];
[maxLength, index] = max(allLengths)
maxLength = 6
index = 11
% Sum the values according to the row number.
% In R_3, the maximum number of consecutive days is 6 days and this would be rows 106 to 111.
indexes = props(index).PixelIdxList
indexes = 6×1
106 107 108 109 110 111
% I want to sum the values in rows 106 to 111.
theSum = sum(R_3(indexes))
theSum = 97.5360

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

### カテゴリ

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

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by