Counting program every positive element
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Guillaume
2016-3-5
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.
回答(3 个)
Guillaume
2016-3-5
编辑:Guillaume
2016-3-5
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 个评论
Andrei Bobrov
2016-3-5
编辑:Andrei Bobrov
2016-3-5
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
2016-3-5
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!