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:
error in code

Subject: error in code

From: dwi

Date: 18 Oct, 2012 16:20:15

Message: 1 of 5

I have a vector p and a threshold pthres. I want, starting from the beginning, to compare p with pthres and when p exceeds pthres to start summing up the p values until p falls below pthres for the first time. Then k=1 and the resulting sum corresponds to a value Q1. The next time this happens k=2, Q2 etc. This is what i have so far:
s=0;
i=1;
K=0;
while i<=length(p)
    if p(i)>pthres(i)
        s=s+p(i);
        K=K+1;
    else
        k(i)=K;
        Q(i)=s;
        s=0;
    end
    i=i+1;
end
Q=Q(Q~=0);
k=unique(k);
But then when i plot(k,Q) it says that they don't have the same size. Can anyone see where the mistake is or even if the above code does indeed what I want?Thanks!

Subject: error in code

From: dpb

Date: 19 Oct, 2012 00:39:30

Message: 2 of 5

On 10/18/2012 11:20 AM, dwi wrote:
> I have a vector p and a threshold pthres. I want, starting from the
> beginning, to compare p with pthres and when p exceeds pthres to start
> summing up the p values until p falls below pthres for the first time.
> Then k=1 and the resulting sum corresponds to a value Q1. The next time
> this happens k=2, Q2 etc. This is what i have so far:
> s=0;
> i=1;
> K=0;
> while i<=length(p)
> if p(i)>pthres(i)
> s=s+p(i);
> K=K+1;
,,,

> Can anyone see where the mistake is or even if the above code does
> indeed what I want?Thanks!

Incrementing K every time isn't right...

 >> x = rand(20,1);
     9.5013
     2.3114
     6.0684
     4.8598
     8.9130
     7.6210
     4.5647
     0.1850
     8.2141
     4.4470
...
     8.9365
 >> xmag=4;
 >> ix=x>xmag;
 >> n=length(x)-sum(ix)+(ix(end)~=0); % compute number of sums to find...
 >> s=zeros(n,1); % preallocate for them
 >> j=1;for i=1:length(x),if x(i)>xmag,s(j)=s(j)+x(i);else,j=j+1;end,end
 >> s =
     9.5013
    32.0269
    43.3350
    35.6200
 >> sum(x(3:7))

ans =
    32.0269
 >>

I'm sure w/ new functions of accumarray and friends probably more
concise way is doable but the "dead-ahead" way works, too... :)

The first value is correct by inspection as there's only one element in
the sum; the second group is seen to be the 3:7 entries so the sum()
checks w/ the second result...

--

Subject: error in code

From: dpb

Date: 19 Oct, 2012 00:46:29

Message: 3 of 5

On 10/18/2012 7:39 PM, dpb wrote:
...

> >> j=1;for i=1:length(x),if x(i)>xmag,s(j)=s(j)+x(i);else,j=j+1;end,end
...

Oh, forgot to add--if the first entry is not greater than threshold need
to hold off incrementing j -- easiest way to fix up is to start the loop
at i==first x>threshold.

iStrt=find(x>xmag,1);
j=1;for i=iStrt:length(x),if x(i)>xmag,s(j)=s(j)+x(i);else,j=j+1;end,end

--

Subject: error in code

From: dpb

Date: 19 Oct, 2012 18:09:40

Message: 4 of 5

On 10/18/2012 7:46 PM, dpb wrote:
> On 10/18/2012 7:39 PM, dpb wrote:
> ...
>
>> >> j=1;for i=1:length(x),if x(i)>xmag,s(j)=s(j)+x(i);else,j=j+1;end,end
> ...
>
> Oh, forgot to add--if the first entry is not greater than threshold need
> to hold off incrementing j -- easiest way to fix up is to start the loop
> at i==first x>threshold.
>
> iStrt=find(x>xmag,1);
> j=1;for i=iStrt:length(x),if x(i)>xmag,s(j)=s(j)+x(i);else,j=j+1;end,end
>

OBTW, the same problem will occur when there's more than one consecutive
value below the threshold in the middle of the vector--just got lucky in
the values I chose before that the runs were only separated by one value.

Do the same thing as before; after finish each group have another loop
that spins until it finds the next trigger. Alternatively, could recast
to save indices of first exceedances throughout the vector--diff() is
good for that on the x>thresh vector; look for the +1 locations and then
loop over the collection of those as starting indices. If go that
route, might as well save the ending ones as well. There's always the
end effects to consider of starting and stopping in a run...

--

Subject: error in code

From: dwi

Date: 19 Oct, 2012 22:21:08

Message: 5 of 5

dpb <none@non.net> wrote in message <k5s50p$qth$1@speranza.aioe.org>...
> On 10/18/2012 7:46 PM, dpb wrote:
> > On 10/18/2012 7:39 PM, dpb wrote:
> > ...
> >
> >> >> j=1;for i=1:length(x),if x(i)>xmag,s(j)=s(j)+x(i);else,j=j+1;end,end
> > ...
> >
> > Oh, forgot to add--if the first entry is not greater than threshold need
> > to hold off incrementing j -- easiest way to fix up is to start the loop
> > at i==first x>threshold.
> >
> > iStrt=find(x>xmag,1);
> > j=1;for i=iStrt:length(x),if x(i)>xmag,s(j)=s(j)+x(i);else,j=j+1;end,end
> >
>
> OBTW, the same problem will occur when there's more than one consecutive
> value below the threshold in the middle of the vector--just got lucky in
> the values I chose before that the runs were only separated by one value.
>
> Do the same thing as before; after finish each group have another loop
> that spins until it finds the next trigger. Alternatively, could recast
> to save indices of first exceedances throughout the vector--diff() is
> good for that on the x>thresh vector; look for the +1 locations and then
> loop over the collection of those as starting indices. If go that
> route, might as well save the ending ones as well. There's always the
> end effects to consider of starting and stopping in a run...
>
> --
Thank you for your time! I'll work on this tomorrow.

Tags for this Thread

No tags are associated with 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