counting data points
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
1 Kommentar
Akzeptierte Antwort
Image Analyst
am 25 Apr. 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]
Weitere Antworten (2)
per isakson
am 24 Apr. 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 Kommentare
Andrei Bobrov
am 25 Apr. 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 Kommentare
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!