Thread Subject: if statement help

Subject: if statement help

From: Adam Bright

Date: 25 Apr, 2008 01:23:02

Message: 1 of 9

Hi I'm having trouble composing an if statement to say if
the difference between the next value and the current value
is gretaer or equal to 150 & is less then the next value,
then for this value and the next 5 values, to use only the
previous value.however I then also need it so that after the
5 values using the previous value; any values after this use
the difference between the two values + the initial previosu
value. (if that makes any sense).

For example:
1793.00 1793.00
1810.00 1810.00
1827.00 1827.00
1845.00 1845.00
1861.00 1861.00
1876.00 1876.00
1891.00 1891
1686.00 1891
1178.00 1891
709.90 1891
722.90 1891
771.70 1891
818.00 1937.3 ((818-771.7)+1891)
861.00 1980.3 ((861-818)+1937.3)
904.00 2023.3 ((904-861)+1980.3)

The same process might need to occur further down the array
several times, so it needs to be some kind of statement
which can control it in the above manner.So far I've managed
to write an if statement without knowing how to get it to
apply to the future values and getting the difference:

for i = 2:length(pressure),
    if ((pressure(i) <
pressure(i-1))&&((pressure(i-1)-pressure(i))>=150))
        ((pressure(i) = pressure(i-1))&&...
            (pressure(i+1) = pressure(i-1))&&(pressure(i+2)
= pressure(i-1))&&(pressure(i+3)...
            = pressure(i-1))&&(pressure(i+4) =
pressure(i-1))&&(pressure(i+5) = pressure(i-1)));
    end,
end

thanks for your help

Subject: if statement help

From: Peter Boettcher

Date: 25 Apr, 2008 13:27:13

Message: 2 of 9

"Adam Bright" <abc_services_2000@yahoo.co.uk> writes:

> Hi I'm having trouble composing an if statement to say if
> the difference between the next value and the current value
> is gretaer or equal to 150 & is less then the next value,
> then for this value and the next 5 values, to use only the
> previous value.however I then also need it so that after the
> 5 values using the previous value; any values after this use
> the difference between the two values + the initial previosu
> value. (if that makes any sense).
>
> For example:
> 1793.00 1793.00
> 1810.00 1810.00
> 1827.00 1827.00
> 1845.00 1845.00
> 1861.00 1861.00
> 1876.00 1876.00
> 1891.00 1891
> 1686.00 1891
> 1178.00 1891
> 709.90 1891
> 722.90 1891
> 771.70 1891
> 818.00 1937.3 ((818-771.7)+1891)
> 861.00 1980.3 ((861-818)+1937.3)
> 904.00 2023.3 ((904-861)+1980.3)
>
> The same process might need to occur further down the array
> several times, so it needs to be some kind of statement
> which can control it in the above manner.So far I've managed
> to write an if statement without knowing how to get it to
> apply to the future values and getting the difference:
>
> for i = 2:length(pressure),
> if ((pressure(i) <
> pressure(i-1))&&((pressure(i-1)-pressure(i))>=150))
> ((pressure(i) = pressure(i-1))&&...
> (pressure(i+1) = pressure(i-1))&&(pressure(i+2)
> = pressure(i-1))&&(pressure(i+3)...
> = pressure(i-1))&&(pressure(i+4) =
> pressure(i-1))&&(pressure(i+5) = pressure(i-1)));
> end,
> end

I would suggest a "while" loop instead, so you can modify the index
inside the loop to take care of that group of 5.

while(i < length(pressure)-5)
  if( <your condition> )
    for j=0:4
      pressure(i+j) = <old value>;
    end
    i = i + 5;
  else
    apply adjustment to current value
    i = i + 1;
  end

end

In other words, "i" always points to the output value.

-Peter

Subject: if statement help

From: Adam Bright

Date: 25 Apr, 2008 15:02:01

Message: 3 of 9

thanks peter for your reply,I kind of see how this would
work,however I dont understand why you have given j=0.4 the
difference between the two will not always be the same,
however I will always want the current value and the next 5
values to use the 1st previous value and thereafter to use
this value + the differences gained .

Please could you explain further so i understand it
better.many thanks once again

Subject: if statement help

From: Peter Boettcher

Date: 25 Apr, 2008 15:56:52

Message: 4 of 9

"Adam Bright" <abc_services_2000@yahoo.co.uk> writes:

> thanks peter for your reply,I kind of see how this would
> work,however I dont understand why you have given j=0.4 the
> difference between the two will not always be the same,
> however I will always want the current value and the next 5
> values to use the 1st previous value and thereafter to use
> this value + the differences gained .
>
> Please could you explain further so i understand it
> better.many thanks once again

Please quote from previous posts for context, and reply to the post you
are actually replying to, so that threading works correctly.

I never used 0.4. for j=0:4 is a loop of j from 0 to 4. It is a way to
make the "next 5 values" the same as the input.

Copy of the code:

while(i < length(pressure)-5)
  if( <your condition> )
    for j=0:4
      pressure(i+j) = <old value>;
    end
    i = i + 5;
  else
    apply adjustment to current value
    i = i + 1;
  end
end


-Peter

Subject: if statement help

From: Adam Bright

Date: 25 Apr, 2008 16:54:01

Message: 5 of 9

I'm sorry Peter,i'm extremely grateful for your help,but I
can't get it to work.

while(i < length(pressure)-5)
  if( ((pressure(i) <
pressure(i-1))&&((pressure(i-1)-pressure(i))>=150)) )
    for j=0:4
      pressure(i+j) = pressure(i-1);
    end
    i = i + 5;
  else
    apply adjustment to current value
    i = i + 1;
  end
end

I'm not too sure what I would put where you have written
apply adjustment to current value, would this be
((pressure(i+6)-pressure(i+5)) + pressure(i))?
i've tried a few variations but have'nt got it to work.


Peter Boettcher <boettcher@ll.mit.edu> wrote in message
<muytzhqkjwb.fsf@G99-Boettcher.llan.ll.mit.edu>...
> "Adam Bright" <abc_services_2000@yahoo.co.uk> writes:
>
> > thanks peter for your reply,I kind of see how this would
> > work,however I dont understand why you have given j=0.4 the
> > difference between the two will not always be the same,
> > however I will always want the current value and the next 5
> > values to use the 1st previous value and thereafter to use
> > this value + the differences gained .
> >
> > Please could you explain further so i understand it
> > better.many thanks once again
>
> Please quote from previous posts for context, and reply to
the post you
> are actually replying to, so that threading works correctly.
>
> I never used 0.4. for j=0:4 is a loop of j from 0 to 4.
It is a way to
> make the "next 5 values" the same as the input.
>
> Copy of the code:
>
> while(i < length(pressure)-5)
> if( <your condition> )
> for j=0:4
> pressure(i+j) = <old value>;
> end
> i = i + 5;
> else
> apply adjustment to current value
> i = i + 1;
> end
> end
>
>
> -Peter

Subject: if statement help

From: Peter Boettcher

Date: 25 Apr, 2008 17:49:34

Message: 6 of 9

"Adam Bright" <abc_services_2000@yahoo.co.uk> writes:

> I'm sorry Peter,i'm extremely grateful for your help,but I
> can't get it to work.
>
> while(i < length(pressure)-5)
> if( ((pressure(i) <
> pressure(i-1))&&((pressure(i-1)-pressure(i))>=150)) )
> for j=0:4
> pressure(i+j) = pressure(i-1);
> end
> i = i + 5;
> else
> apply adjustment to current value
> i = i + 1;
> end
> end
>
> I'm not too sure what I would put where you have written
> apply adjustment to current value, would this be
> ((pressure(i+6)-pressure(i+5)) + pressure(i))?
> i've tried a few variations but have'nt got it to work.

Sorry, I can't help further. Pretend you're a computer, remember that i
will always point at the current *output* value and step-by-step go
through it. It might help to do an example by hand, at all times
keeping track of the value of i, and figuring out what offsets against i
you need to apply to get to the numbers you need.

-Peter


> Peter Boettcher <boettcher@ll.mit.edu> wrote in message
> <muytzhqkjwb.fsf@G99-Boettcher.llan.ll.mit.edu>...
>> "Adam Bright" <abc_services_2000@yahoo.co.uk> writes:
>>
>> > thanks peter for your reply,I kind of see how this would
>> > work,however I dont understand why you have given j=0.4 the
>> > difference between the two will not always be the same,
>> > however I will always want the current value and the next 5
>> > values to use the 1st previous value and thereafter to use
>> > this value + the differences gained .
>> >
>> > Please could you explain further so i understand it
>> > better.many thanks once again
>>
>> Please quote from previous posts for context, and reply to
> the post you
>> are actually replying to, so that threading works correctly.
>>
>> I never used 0.4. for j=0:4 is a loop of j from 0 to 4.
> It is a way to
>> make the "next 5 values" the same as the input.
>>
>> Copy of the code:
>>
>> while(i < length(pressure)-5)
>> if( <your condition> )
>> for j=0:4
>> pressure(i+j) = <old value>;
>> end
>> i = i + 5;
>> else
>> apply adjustment to current value
>> i = i + 1;
>> end
>> end
>>
>>
>> -Peter
>

--

Subject: if statement help

From: Adam Bright

Date: 25 Apr, 2008 22:33:02

Message: 7 of 9

Hi, I've been trying to play around with it but I get the
following error:
??? Subscript indices must either be real positive integers
or logicals.

Error in ==> untitld at 3
  if( ((A(i) < A(i-1))&&((A(i-1)-A(i))>=150)) )

A=[1500;1600;1793.00;1810.00;1827.00;1827.00;1845.00;1861.00;1876.00;1891.00;1686.00;1178.00;709.90;722.90;771.70;818.00;861.00;904.00;]
while(i < length(A)-5)
  if( ((A(i) < A(i-1))&&((A(i-1)-A(i))>=150)) )
    for j=0:4
      A(i+j) = A(i-1);
    end
    i = i + 5;
  else
    (A(i+1)-A(i))+A(i-1)
    i = i + 1;
  end
end


Peter Boettcher <boettcher@ll.mit.edu> wrote in message
<muymynhlt8x.fsf@G99-Boettcher.llan.ll.mit.edu>...
> "Adam Bright" <abc_services_2000@yahoo.co.uk> writes:
>
> > I'm sorry Peter,i'm extremely grateful for your help,but I
> > can't get it to work.
> >
> > while(i < length(pressure)-5)
> > if( ((pressure(i) <
> > pressure(i-1))&&((pressure(i-1)-pressure(i))>=150)) )
> > for j=0:4
> > pressure(i+j) = pressure(i-1);
> > end
> > i = i + 5;
> > else
> > apply adjustment to current value
> > i = i + 1;
> > end
> > end
> >
> > I'm not too sure what I would put where you have written
> > apply adjustment to current value, would this be
> > ((pressure(i+6)-pressure(i+5)) + pressure(i))?
> > i've tried a few variations but have'nt got it to work.
>
> Sorry, I can't help further. Pretend you're a computer,
remember that i
> will always point at the current *output* value and
step-by-step go
> through it. It might help to do an example by hand, at
all times
> keeping track of the value of i, and figuring out what
offsets against i
> you need to apply to get to the numbers you need.
>
> -Peter
>
>
> > Peter Boettcher <boettcher@ll.mit.edu> wrote in message
> > <muytzhqkjwb.fsf@G99-Boettcher.llan.ll.mit.edu>...
> >> "Adam Bright" <abc_services_2000@yahoo.co.uk> writes:
> >>
> >> > thanks peter for your reply,I kind of see how this would
> >> > work,however I dont understand why you have given
j=0.4 the
> >> > difference between the two will not always be the same,
> >> > however I will always want the current value and the
next 5
> >> > values to use the 1st previous value and thereafter
to use
> >> > this value + the differences gained .
> >> >
> >> > Please could you explain further so i understand it
> >> > better.many thanks once again
> >>
> >> Please quote from previous posts for context, and reply to
> > the post you
> >> are actually replying to, so that threading works
correctly.
> >>
> >> I never used 0.4. for j=0:4 is a loop of j from 0 to 4.
> > It is a way to
> >> make the "next 5 values" the same as the input.
> >>
> >> Copy of the code:
> >>
> >> while(i < length(pressure)-5)
> >> if( <your condition> )
> >> for j=0:4
> >> pressure(i+j) = <old value>;
> >> end
> >> i = i + 5;
> >> else
> >> apply adjustment to current value
> >> i = i + 1;
> >> end
> >> end
> >>
> >>
> >> -Peter
> >
>
> --

Subject: if statement help

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 25 Apr, 2008 22:46:11

Message: 8 of 9

In article <futm6u$6tc$1@fred.mathworks.com>,
Adam Bright <abc_services_2000@yahoo.co.uk> wrote:
>Hi, I've been trying to play around with it but I get the
>following error:
>??? Subscript indices must either be real positive integers
>or logicals.

>Error in ==> untitld at 3
> if( ((A(i) < A(i-1))&&((A(i-1)-A(i))>=150)) )

>A=[1500;1600;1793.00;1810.00;1827.00;1827.00;1845.00;1861.00;1876.00;1891.00;1686.00;1178.00;709.90;722.90;771.70;818.00;861.00;904.00;]
>while(i < length(A)-5)
> if( ((A(i) < A(i-1))&&((A(i-1)-A(i))>=150)) )

What is 'i' initialized to? There is no initialization shown here.
Could i be 0? Could i be 1? If i is 1 then A(i-1) would be A(0)
and 0 is not a real positive integer
--
   "Okay, buzzwords only. Two syllables, tops." -- Laurie Anderson

Subject: if statement help

From: Adam Bright

Date: 25 Apr, 2008 23:31:01

Message: 9 of 9

Hi Walter,
is it possible you could help me further?
It appears that the script is working as there are no erros
showing but now,the matlab says its 'busy' and after 10 mins
is still not showing and graph display or error, this is the
code i used:

i=2; newpressure(1:length(pressure))=0;
while i <= length(pressure)
    if ((newpressure(i) <
newpressure(i-1))&&((newpressure(i-1)-newpressure(i))>=150))
   newpressure(i:i+5)=pressure(i);
    end
   i=1+5;

for j=i:length(pressure)

newpressure(j)=(pressure(j)-pressure(j-1))+newpressure(j-1);
end
i=1+1;
end

i've then continued with my script:
case1 = (newpressure < 673.2);
case2 = (newpressure < 995.7);
case3 = (newpressure >= 995.7 & newpressure < 1149.2);
case4 = (newpressure >= 1149.2);
volume = zeros(size(newpressure));
volume(case1) = 0;
volume(case2) = ((newpressure(case2)-442.95)*5.365);
volume(case3) = ((newpressure(case3)-749.29)*12.034);
volume(case4) = ((newpressure(case4)-934.08)*22.371);


roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <futmvj$ncp$1@canopus.cc.umanitoba.ca>...
> In article <futm6u$6tc$1@fred.mathworks.com>,
> Adam Bright <abc_services_2000@yahoo.co.uk> wrote:
> >Hi, I've been trying to play around with it but I get the
> >following error:
> >??? Subscript indices must either be real positive integers
> >or logicals.
>
> >Error in ==> untitld at 3
> > if( ((A(i) < A(i-1))&&((A(i-1)-A(i))>=150)) )
>
>
>A=[1500;1600;1793.00;1810.00;1827.00;1827.00;1845.00;1861.00;1876.00;1891.00;1686.00;1178.00;709.90;722.90;771.70;818.00;861.00;904.00;]
> >while(i < length(A)-5)
> > if( ((A(i) < A(i-1))&&((A(i-1)-A(i))>=150)) )
>
> What is 'i' initialized to? There is no initialization
shown here.
> Could i be 0? Could i be 1? If i is 1 then A(i-1) would be
A(0)
> and 0 is not a real positive integer
> --
> "Okay, buzzwords only. Two syllables, tops." -- Laurie
Anderson

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
logic Adam Bright 24 Apr, 2008 21:25:06
statement Adam Bright 24 Apr, 2008 21:25:06
if Adam Bright 24 Apr, 2008 21:25:05
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com