Count maximum length of values < 1 in matrix
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Eli
am 10 Dez. 2022
Kommentiert: Eli
am 10 Dez. 2022
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 Kommentare
Akzeptierte Antwort
Walter Roberson
am 10 Dez. 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))
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!