starting location of vector
4 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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 个评论
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
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})]
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
0 个评论
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]
0 个评论
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]
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!