필터 지우기
필터 지우기

Sum values on the maximum number of consecutive days

조회 수: 2 (최근 30일)
Eli
Eli 2023년 6월 26일
답변: Image Analyst 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;
load('Sample.mat');
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
KSSV 2023년 6월 26일
load Sample.mat ;
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

Image Analyst
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).
load('Sample.mat');
% 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 CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by