Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
finding list of numbers

Subject: finding list of numbers

From: Jessica

Date: 18 Jun, 2008 02:00:23

Message: 1 of 4

Hi,

Does anyone have any suggestions for finding a clump of
numbers in a column of data?

For example, I have a column:

3
4
5
1
87
6
4
745
0
0
0
0
0
0
5
4
3
5
7
53
2
0
0
0
9
6
5
.
.
.

and I would like to be able to identify both clumps of
zeros by having their start and stop row numbers.
Thus, I would hope to output something that told me that
the first set of zeros is from rows 9-14 and the second set
from 22-24.

Thanks!

Subject: finding list of numbers

From: dpb

Date: 18 Jun, 2008 03:27:37

Message: 2 of 4

Jessica wrote:
...
> and I would like to be able to identify both clumps of
> zeros by having their start and stop row numbers.

As a starting point,

z = find(x==0); % find the locations of zeroes
dz = diff(z) ; % if dz==1, consecutive zeroes

Work back from initial location of where dz==1 occurs to the
corresponding location in z. The first in a sequence will be one less
than the location in dz and the last the last.

--

Subject: finding list of numbers

From: Jessica

Date: 18 Jun, 2008 19:52:02

Message: 3 of 4

dpb <none@non.net> wrote in message
<g39vfm$fnj$1@aioe.org>...
> Jessica wrote:
> ...
> > and I would like to be able to identify both clumps of
> > zeros by having their start and stop row numbers.
>
> As a starting point,
>
> z = find(x==0); % find the locations of zeroes
> dz = diff(z) ; % if dz==1, consecutive zeroes
>
> Work back from initial location of where dz==1 occurs to
the
> corresponding location in z. The first in a sequence
will be one less
> than the location in dz and the last the last.
>
> --

Great suggestion. I got it to work!

Subject: finding list of numbers

From: us

Date: 18 Jun, 2008 20:09:02

Message: 4 of 4

"Jessica ":
<SNIP gap-finder evergreen...

one of the many solutions

% the data
     d=[0,1,2,0,0,3,4,0,6,0];
% the engine
     dd=[true,d~=0,true];
     ixb=strfind(dd,[1,0]);
     ixe=strfind(dd,[0,1])-1;
% the result
     r=[ixb;ixe;ixe-ixb+1]
%{
     1 4 8 10 % start
     1 5 8 10 % end
     1 2 1 1 % #zeros
%}

us

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us