How to count continuous non zero elements whilst keeping the date of the first non-zero value

1 次查看(过去 30 天)
I a have a set of data in a matrix whereby the first 5 columns represent: year month day hour minute The 6th column represents a magnitude value and I want to know how can I continuously count the size of the non zero values enclosed by two zeros and return it in a 7th column in the same row as where the first non zero value is located.
In other words, I have this:
2010 1 1 0 5 0
2010 1 1 0 10 6
2010 1 1 0 15 16
2010 1 1 0 20 0
2010 1 1 0 25 12
2010 1 1 0 30 19
2010 1 1 0 35 11
2010 1 1 0 40 0
and I want this:
2010 1 1 0 5 0
2010 1 1 0 10 6 2
2010 1 1 0 15 16
2010 1 1 0 20 0
2010 1 1 0 25 12 3
2010 1 1 0 30 19
2010 1 1 0 35 11
2010 1 1 0 40 0
Many thanks!

采纳的回答

Sergey Kasyanov
Sergey Kasyanov 2018-4-6
Hi.
It's the simplest but not the shortest code for this problem.
k=A(1,6)~=0;
for i=1:size(A,1)
if A(i,6)~=0&k==0
k=i;
elseif A(i,6)==0&k~=0
A(k,7)=i-k;
k=0;
end
end
if k~=0
A(end,7)=i-k+1;
end
  4 个评论
Sergey Kasyanov
Sergey Kasyanov 2018-4-6
Of course.
k=A(1,6)~=0;
for i=1:size(A,1)
if A(i,6)~=0&k==0
k=i;
elseif A(i,6)==0&k~=0
A(k,7)=i-k;
A(k,8)=max(A(k:i,6));
k=0;
end
end
if k~=0
A(k,7)=i-k+1;
A(k,8)=max(A(k:i,6))
end

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by