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
  1 件のコメント
Eli
Eli 2022 年 12 月 10 日
Thank you!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing 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