starting location of vector
Afficher commentaires plus anciens
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.
Réponse acceptée
Plus de réponses (4)
Azzi Abdelmalek
le 5 Mai 2016
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 commentaire
Monika Kok
le 5 Mai 2016
Weird Rando
le 5 Mai 2016
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
le 5 Mai 2016
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
le 5 Mai 2016
Modifié(e) : Image Analyst
le 5 Mai 2016
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]
Catégories
En savoir plus sur Transforms dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!