COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### How to 'detect' zeros between two non-zeros numbers ?

• Email
• Follow

```Hi everyone,

I'm trying to build an algorithm so that i could know if in any vector there is a zero (or many) between 2 non-zeros numbers.

For example :

x=[0 0 0 0 1 2 3 0 0 4 0 0 0]

How could i know that there is 2 zeros between 3 and 4 ? The zeros at the beginning and the end don't interest me. Any suggestions ?

Thank you
```
 0

See related articles to this posting

```"MOUIZINA Ali" <faytstrife@hotmail.fr> wrote in message <i431gq\$9qd\$1@fred.mathworks.com>...
> Hi everyone,
>
> I'm trying to build an algorithm so that i could know if in any vector there is a zero (or many) between 2 non-zeros numbers.
>
> For example :
>
> x=[0 0 0 0 1 2 3 0 0 4 0 0 0]
>
> How could i know that there is 2 zeros between 3 and 4 ? The zeros at the beginning and the end don't interest me. Any suggestions ?
>
> Thank you

hi

one approach:

pos=find(x~=0);
i=pos(1) : (pos(end)-1); %ignore zeros at start and end
s=find(x(i)=~0 & x(i+1)==0); %find position of start of all runs of zero
e=find(x(i)==0 & x(i+1)~=0); %find position of end of all runs of zero
len=e-s+1; %length of each run of zeros

(warnings - not tested & not guaranteed to be fast)

Ross

```
 0

```Dear MOUIZINA Ali,
(Sorry, I cannot identify your first name)

> I'm trying to build an algorithm so that i could know if in any vector there is a zero (or many) between 2 non-zeros numbers.
> x = [0 0 0 0 1 2 3 0 0 4 0 0 0]

First step: reduce the precision to LOGICAL:
a = (x ~= 0);
% ==> [0 0 0 0 1 1 1 0 0 1 0 0 0]
Now your condition needs the appearence of [1, 0] and [0, 1]:
OneZero = strfind(a, [true, false]);
if length(OneZero) > 1    % Early reply: [..., 1,0 ..., 1,0, ...]
aContainsSurroundedZeros = true;
elseif isempty(OneZero)
aContainsSurroundedZeros = false;
else  % OneZero is a scalar: Exclude [0, 1, 0, 0]
ZeroOne = strfind(a, [false, true]);
aContainsSurroundedZeros = (OneZero < ZeroOne(end));
end

```
 0

2 Replies
303 Views

Similar Articles

12/2/2013 12:38:44 AM
page loaded in 862463 ms -1