Count maximum length of values < 1 in matrix

조회 수: 2 (최근 30일)
Eli
Eli 2022년 12월 10일
댓글: Eli 2022년 12월 10일
Dear all,
I am interested in determining the maximum length of values < 1 in A. E.g., the length of values < 1 in A: 1 at A(1), 10 from A(3:12), and 17 from A(15:end). The maximum length I should get is 17 from A(15:end).
A = [0 6.858 0 0 0 0 0 0 0 0 0 0.762 5.334 2.54 0 0 0 0 0 0 0 0.762 0 0 0 0 0 0 0 0 0.508];
rdc = find(A > 1); % Returns the index of value > 1
CDD_m = max(diff(rdc))-1; % Calculates the backward difference in rdc. The maximum value represents the maximum length of A<1.
  1. rdc gives [2 13 14] which coincides with the values [6.858 5.334 2.54].
  2. CDD_m gives 10.
However, 10 is incorrect as it is not the maximum length of values < 1. Any ideas on how to solve this issue?
Thank you.

채택된 답변

Walter Roberson
Walter Roberson 2022년 12월 10일
A = [0 6.858 0 0 0 0 0 0 0 0 0 0.762 5.334 2.54 0 0 0 0 0 0 0 0.762 0 0 0 0 0 0 0 0 0.508];
mask = A < 1;
starts = strfind([false, mask], [0 1]);
stops = strfind([mask, false], [1 0]);
[len, idx] = max(stops-starts+1);
fprintf('longest run is %d starting from %d going to %d\n', len, starts(idx), stops(idx))
longest run is 17 starting from 15 going to 31

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by