Borrar filtros
Borrar filtros

split binary array based on value 1

2 visualizaciones (últimos 30 días)
Jasper
Jasper el 31 de Ag. de 2015
Respondida: Azzi Abdelmalek el 31 de Ag. de 2015
Hi,
I have a binary array that I would like to split up into several others, each starting with a '1'. I almost have it to work, except the sizes do not match and mat2cell does not work. Please help me with the last bit, thanks!
Jasper
a = [ 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0]';
indices = find(a(:,1)~= 0); % gives 1,5,11,16 and is correct
sizes = diff([indices' size(a, 1)]);% gives 4,6,5,13 instead of 4,6,5,14!
mat2cell(a, sizes, size(a, 1)); % fail!

Respuesta aceptada

Walter Roberson
Walter Roberson el 31 de Ag. de 2015
sizes = diff([indices' size(a, 1)+1])

Más respuestas (2)

Image Analyst
Image Analyst el 31 de Ag. de 2015
I don't see any splitting going on - it's not splitting the array into multiple other arrays. What I see is you counting the zeros and ones as a unit. If you have the Image Processing toolbox, you can just find the sizes of the 0's and add 1:
a = [ 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0]';
labeledArray = bwlabel(a==0);
measurements = regionprops(labeledArray, 'Area');
lengths = [measurements.Area]+1
In the command window:
lengths =
4 6 5 14

Azzi Abdelmalek
Azzi Abdelmalek el 31 de Ag. de 2015
a = [ 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0]';
ii1=strfind(a',[1 0]);
ii2=strfind([a' 1],[0 1]);
out=arrayfun(@(x,y) a(x:y),ii1,ii2,'un',0);
celldisp(out)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by