starting location of vector

조회 수: 5 (최근 30일)
Monika  Kok
Monika Kok 2016년 5월 5일
편집: Image Analyst 2016년 5월 5일
i am using the code to find maximum zero span
g = [ 1 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1 ]
pos1 = find(g==1);
zero_span = diff(pos1)-1;
max_zero_span = max(zero_span)
as from vector g we can see that maximum zero span is starting from location 3. how can i find this with the help of MATLAB.

채택된 답변

Guillaume
Guillaume 2016년 5월 5일
편집: Guillaume 2016년 5월 5일
This is a variation on run length encoding, for which you'll find plenty of functions on the FileExchange.
In any case, it's trivial to do, if you diff your vector, you'll have -1 for transitions from 1 to 0, +1 for transitions from 0 to 1 and 0 elsewhere. The difference between find on the 1 and -1 will give you the lengths of the runs. To make sure that you find the start of a zero run that starts from the beginning of the vector, prepend it with a 1. Same for the end:
g = [ 1 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1 ];
transitions = diff([1 g 1]);
runstarts = find(transitions == -1);
runends = find(transitions == 1);
%because of the 1 padding you're guaranteed there's always the same number of starts and ends
runlengths = runends - runstarts;
[maxlength, runindex] = max(runlengths);
fprintf('\nThe longest run has length %d, starting at index %d\n\n', maxlength, runstarts(runindex))
  댓글 수: 4
Monika  Kok
Monika Kok 2016년 5월 5일
편집: Guillaume 2016년 5월 5일
actually in some cases i am using the
g = [ 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 ];
in such cases i am using end around zero span as well. like in this case max zero span is of length 11. if i do padding than it will change my result
Guillaume
Guillaume 2016년 5월 5일
편집: Guillaume 2016년 5월 5일
Well your original code certainly didn't account for that. Padding or no padding. There are many ways to cope with this. Probably the simplest is to temporary rotate the vector so it always starts with 1:
g = [ 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 ];
shift = find(g, 1); %find 1st 1
transitions = diff([circshift(g, 1-shift, 2), 1]); %padding after still required.
runstarts = find(transitions == -1) + shift;
runends = find(transitions == 1) + shift; %note that runends may be greater than numel(g) if the run wraps around
runlengths = runends - runstarts;
[maxlength, runindex] = max(runlengths);
fprintf('\nThe longest run has length %d, starting at index %d\n\n', maxlength, runstarts(runindex))
Left as an exercise to the reader is coping with the special case where g is all zeros (which will make the circshift fail since shift will be empty)

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 5월 5일
g = [ 1 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1 ]
[~,~,ii]=unique(cumsum(g).*not(g))
d=accumarray(ii,(1:numel(ii))',[],@(x) {x});
dd=d(2:end);
[~,mx]=max(cellfun(@numel,dd));
indices=[min(dd{mx}) max(dd{mx})]
  댓글 수: 1
Monika  Kok
Monika Kok 2016년 5월 5일
I want a simpler solution using for loop's. I can not use such complex statements. please

댓글을 달려면 로그인하십시오.


Weird Rando
Weird Rando 2016년 5월 5일
This is the most simplest solution I came up with. Using the find function and subtract the positions from the next one.
g = [ 1 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1 ];
pos = find(g == 1);
nloop = length(pos)-1;
maxspan = 0;
for i = 1:nloop
span = pos(i+1)-pos(i)-1
if span > maxspan
maxspan = span;
end
end

Jan
Jan 2016년 5월 5일
data = [ 1 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1 ]
[b, n, ind] = RunLength(data);
ind = ind(b == 0);
[len, pos] = max(n(b == 0));
index = ind(pos); % index related to [data]

Image Analyst
Image Analyst 2016년 5월 5일
편집: Image Analyst 2016년 5월 5일
I don't know how you get 3. There is not even a zero at index 3. I get 4 with 3 simple lines of code (not including comments) that you can use if you have the Image Processing Toolbox.
% Define sample data
g = [ 1 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1 ]
% Get run lengths of the zero regions and their locations.
stats = regionprops(logical(~g), 'Area', 'PixelIdxList');
% Get the max length of all the zero regions
[maxRunLength, regionNumber] = max([stats.Area])
% Get the starting index of the longest zero region.
indexOfLongestRun = stats(regionNumber).PixelIdxList(1)
In the command window you will see
g =
1 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1
maxRunLength =
9
regionNumber =
1
indexOfLongestRun =
4
If you happen to want to get all the lengths of all the zero regions you can do this:
allZeroRunLengths = [stats.Area]

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by