Sum of highest length of consecutive lowest values from a array.
조회 수: 16(최근 30일)
A = [5 1 1 1 6 1 1 1 1 1 1 1 7 1 1 1 7]
I would like to sum the highest consecutive numbers those are less than 2. In this case, there three 1 at the beginning and at the end but those will not be counted because there are 7 consecutive 1 at the middle. And I am looking for highest consecutive numbers those are less than 2 that's why the answer would be 1+1+1+1+1+1+1=7.
Image Analyst 2018년 4월 9일
You could do this:
A = [5 1 1 1 6 1 1 1 1 1 1 1 7 1 1 1 7] % Assume A is all integers.
% Get unique integers in A
ua = unique(A)
for k = 1 : length(ua)
thisNumber = ua(k)
% Measure lengths of all regions comprised of this number.
props = regionprops(A==thisNumber, A, 'Area')
% Get maximum length for this number in A.
maxAreas(k) = max([props.Area])
maxAreas % Show in command window.
Roger Stafford 2018년 4월 11일
편집: Roger Stafford 2018년 4월 11일
L = min(A);
f = find(diff([false,A==L,false])~=0);
h = L*max(f(2:2:end)-f(1:2:end-1));
h is the highest sum of consecutive lowest values.
[Note: You should be careful how your fractions are generated. You might have, say, two consecutive values which appear equal to the minimum value, .2, .2, but which are not exactly equal. These would not be detected as part of a consecutive sequence of least values.]