Count maximum length of values < 1 in matrix

2 views (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 10 Dec 2022
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

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!