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 个评论
Milosz Mart
Milosz Mart 2016-3-5
clear all; close all;
filename = 'C:\[...]\kwazi_data_1993.txt'
delimiter = '\t';
formatSpec = '%f%f%f%f%f%f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'EmptyValue' ,NaN, 'ReturnOnError', false);
fclose(fileID);
data = dataArray{:, 1};
clearvars filename delimiter formatSpec fileID dataArray ans;
filename = 'C:\[...]\kwazi_data_1993.txt';
delimiter = '\t';
formatSpec = '%{yyyyMMddHH}D%*s%*s%*s%*s%*s%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'EmptyValue' ,NaN, 'ReturnOnError', false);
fclose(fileID);
DATA = dataArray{:, 1};
clearvars filename delimiter formatSpec fileID dataArray ans;
filename = 'C:\[...]\calk1993.txt'
delimiter = '\t';
formatSpec = '%f%f%f%f%f%f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'EmptyValue' ,NaN, 'ReturnOnError', false);
fclose(fileID);
VarName1 = dataArray{:, 1};
clearvars filename delimiter formatSpec fileID dataArray ans;
for k=1:365
if VarName1(1:k,1)>0
KKK=VarName1(1:k,1);
else
KKK=VarName1(1:k,1)>=0
end
end
KK=KKK.*VarName1
Guillaume
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
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

Andrei Bobrov
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
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.

标签

Community Treasure Hunt

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

Start Hunting!

Translated by