Prev: slow drawing on figure with many GUI elements
Next: RMS calculation in frequency domain after windowing
From: MOUIZINA Ali on 13 Aug 2010 04:59 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
From: Ross W on 13 Aug 2010 05:48 "MOUIZINA Ali" <faytstrife(a)hotmail.fr> wrote in message <i431gq$9qd$1(a)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
From: Jan Simon on 13 Aug 2010 10:25
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 Kind regrads, Jan |