counting data points
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have a data set of 1's and 0's like this 000001111100000111110000011111000001111100000 this data set is called "c"
I need the program to output a count of sections so to speak. In this example they are all even and short but in my data set they are extremely long and of varying lengths. If I could get an output like 5,5,5,5,5,5,5 counting each section it would save me tons of time.
Respuesta aceptada
Image Analyst
el 25 de Abr. de 2012
If you have the "c" as an integer or logical array, and if you have the Image Processing Toolbox, you can do it in one line with regionprops(). Just simply say "measurements = regionprops(~c, 'Area')" Here's a full demo:
% Create sample data.
c = [0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0]
% Measure the zero sections - KEY LINE OF CODE RIGHT HERE!
measurements = regionprops(~c, 'Area')
% Display results.
fprintf('The number of zero sections is %d.\n', length(measurements));
fprintf('The sizes of zero sections are:\n');
allAreas = [measurements.Area]
Más respuestas (2)
per isakson
el 24 de Abr. de 2012
I learned this trick the other day:
str = '11110111000001111100000111110000011111000001111100000';
cac = regexp( str, '0+', 'split' );
n = cellfun( @(s) length(s), cac )
n =
4 3 5 5 5 5 0
You might want to check for leading and trailing 0. If the string is huge you might need to split it.
--- EDIT without the Image Processing Toolbox ---
c = round( rand( 1,100 ) + 0.1 ); % Sample data
str = sprintf( '%1u', c );
cac = regexp( str, '0+', 'split' );
len = cellfun( @(s) length(s), cac );
0 comentarios
Andrei Bobrov
el 25 de Abr. de 2012
str = '11110111000001111100000111110000011111000001111100000';
x = str -'0';
k = find([true, diff(x)~=0]);
out = [x(k); diff([k;[k(2:end),numel(x)+1]])]
OR
out = diff([find([true, diff(x)~=0]) numel(x)+1]);
ADDED with use regexp
out = cellfun('length',regexp(str,'(0*|1*)','match'))
0 comentarios
Ver también
Categorías
Más información sobre Data Type Conversion en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!