Detect parts of a 0/1 (binary) vector consisting of only 0 or 1
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ilya
el 3 de Abr. de 2014
Let's say I have a vector of 0 and 1:
a=[1 1 1 0 0 0 1 0 1 0 0 1 1 0];
It's needed to find a way to extract the vector's indices so that each group of extracted indices contains a part of a vector consisting of only zeros or only ones. For the given example it should be like:
[1 3] [4 6] [7] [8] [9] [10 11] [12 13] [14]
I thought about diff(), but some post-processing is definitely required after it...
0 comentarios
Respuesta aceptada
Sean de Wolski
el 3 de Abr. de 2014
idx = find(diff([~a(1) a]))';
pcs = spdiags(rot90([idx-1 idx]));
pcs(end) = numel(a)
3 comentarios
Más respuestas (1)
Image Analyst
el 3 de Abr. de 2014
If you have the Image Processing Toolbox, the 1's are given by the PixelIdxList property. You can do it in one line with a call to regionprops:
a=[1 1 1 0 0 0 1 0 1 0 0 1 1 0];
% Find 1's.
locations = regionprops(logical(a), 'PixelIdxList');
% Print out to command line:
for k = 1 : length(locations)
locations(k).PixelIdxList
end
% Find 0's.
locations = regionprops(logical(~a), 'PixelIdxList');
% Print out to command line:
for k = 1 : length(locations)
locations(k).PixelIdxList
end
1 comentario
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!