Count maximum length of values < 1 in matrix
2 次查看(过去 30 天)
显示 更早的评论
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.
- rdc gives [2 13 14] which coincides with the values [6.858 5.334 2.54].
- 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.
0 个评论
采纳的回答
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))
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!