MATLAB Answers

Sum of highest length of consecutive lowest values from a array.

44 ビュー (過去 30 日間)
Mohammad Hossain
Mohammad Hossain 2018 年 4 月 9 日
編集済み: Roger Stafford 2018 年 4 月 11 日
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.

  0 件のコメント

サインイン to comment.

回答 (3 件)

Image Analyst
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])
end
maxAreas % Show in command window.

  1 件のコメント

Image Analyst
Image Analyst 2018 年 4 月 11 日
My code still works with your new, floating point numbers:
% A = [5 1 1 1 6 1 1 1 1 1 1 1 7 1 1 1 7] % Assume A is all integers.
A = [5 1 1 1 6 1 .5 .8 .55 .65 .95 1 7 1 1 1 7]
% Get unique numbers 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]);
fprintf('%.2f shows up %d times.\n', thisNumber, maxAreas(k));
end
I added an fprintf() to show you the results:
0.50 shows up 1 times.
0.55 shows up 1 times.
0.65 shows up 1 times.
0.80 shows up 1 times.
0.95 shows up 1 times.
1.00 shows up 3 times.
5.00 shows up 1 times.
6.00 shows up 1 times.
7.00 shows up 1 times.
Note the count (area) is only the length of the longest run, not a count of all the times the number appears. Thus 1 gives 3, not 8.

サインイン to comment.


David Fletcher
David Fletcher 2018 年 4 月 9 日
編集済み: David Fletcher 2018 年 4 月 9 日
A = [5 1 1 1 6 1 1 1 1 1 1 1 7 1 1 1 7]
B=int32(A==min(A));
[startInd lastInd]=regexp(char(B+48),'[1]+');
counts=lastInd-startInd+1;
sumContinuous=max(counts)

  4 件のコメント

表示 1 件の古いコメント
David Fletcher
David Fletcher 2018 年 4 月 10 日
No, you only printed integer values in your example so that's what I went with. If you wanted doubles it would have been an idea to put that in your example. Use Image Analyst's answer above. If I get some time later and you're still having problems, I'll amend the code to work with doubles. At first glance you now are saying that you want something different to what you outlined in your question.
Mohammad Hossain
Mohammad Hossain 2018 年 4 月 10 日
Yeah exactly, I am thinking something different from my original question just for better learning. Thanks.
David Fletcher
David Fletcher 2018 年 4 月 10 日
I've just had another look at your 'revised' question, and I'm struggling to make sense of it. When I first saw it, I assumed that you now wanted the sum of all elements <=1 (rather than the largest contiguous length of the lowest value elements that you originally requested). However, I now see that in the list of elements you want summed together, there are only two values of 1 listed. Either this is a mistake, or there is some other weird exclusion factor that you are applying to all the other values of 1. So, in short, I haven't a clue what you now want.

サインイン to comment.


Roger Stafford
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.]

  0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。


Translated by