Counting program every positive element
1 view (last 30 days)
Show older comments
Hello.
I have problem with one program and I even doesn't know how to start.
I have an array which is built with zeros and positive numbers. I want to know how many points there are between starting zero and zero ending of "the isle of positive numbers". To show my issue, I gave the image of one of my arraies and I want to get from it (from array which create this image...) matrix which have 3 columns (1col. start date/start point; 2col. end date/end point; 3col. how many points are between start date and end date N_i=...). The number of the rows is determined by how many "isles of positive numbers" have each array.

3 Comments
Guillaume
on 5 Mar 2016
Please remove all the blank lines you've inserted between each line of code, then select all the code and press the {}Code button. That will make your post a lot more readable.
Answers (3)
Guillaume
on 5 Mar 2016
Edited: Guillaume
on 5 Mar 2016
This is the common problem of finding the run length of a sequence. There are plenty of answers for this on this forum.
First, identify the sequence. For you it's positive numbers,
v = [0, rand(1, 20), -rand(1, 5), rand(1, 12), 0, rand(1,2)]; %demo data.
insequence = v > 0;
Points in the sequence are indicated by 1, points not in the sequence by 0. Therefore the start of a sequence is found when 0 is followed by 1, the end when 1 is followed by 0
transitions = diff([0 insequence 0]);
transitions is 1 for start of sequence, -1 for end of sequence. I've put 0 at each end to make sure you still have [0 1] or [1 0] if the sequence starts or ends with 1.
startsequences = find(transitions == 1)
endsequences = find(transitions == -1) - 1
Because of the 0 added on each end, you're guaranteed to have the same number of startsequences and endsequences
0 Comments
Andrei Bobrov
on 5 Mar 2016
Edited: Andrei Bobrov
on 5 Mar 2016
Let date1 - arrey with your dates, DATA - your data
f = fopen('kwazi_data_1993.txt');
c = textscan(f,'%s','delimiter','\n');
fclose(f);
date1 = c{:};
DATA = importdata('calk1993.txt');
i1 = [(1:numel(DATA))', bwlabel(DATA > 0)];
lo = i1(:,2) > 0;
a = accumarray(i1(lo,2),i1(lo,1),[],@(x){[min(x),max(x),numel(x)]});
b = cat(1,a{:});
out = [date1(b(:,1:2)), num2cell(b(:,3))];
Image Analyst
on 5 Mar 2016
To determine how many positive values are between index1 and index2 of your signal, you can do this
numPositives = sum(yourSignal(index1:index2) > 0);
If you have a bunch of these "runs" of positive values and don't know the starting and stopping indexes, then you can use regionprops to get the areas and indexes
labeledSignal = bwlabel(yourSignal > 0);
measurements = regionprops(labeledSignal, 'Area', 'PixelIdxValues');
allLengths = [measurements.Area];
allLengths will be a list of the lengths of each "run" of positive values.
0 Comments
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!