Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: if statement troubles

Subject: if statement troubles

From: Adam Bright

Date: 21 Feb, 2008 13:06:01

Message: 1 of 3

Hi there, I'm having some trouble trying to write an if
statement that will work for my problem.
Imagine there is data in the following way:

1;2;3;4;5;6;5;7;8;9;8;7;6;5;0;1;2;3

I'm trying to write a statement which says that if the
current value is less than the previous value, than to use
the previous value instead. This will eridicate any anomiles
in the data which I have. However there will be certain
times when the data is actually decreasing and this needs to
be the value used, however due to this being a resetting of
an instrument, the volume hasnt actually decreased just the
recording and the subsequent data will need to be added to
the previous value prior to the value decreasing period.
(e.g 1;2;3; added to 9) I've tried to make a script which
states to use the current value if it is greater than
the previous or that the three consequtive values are less
than the one before (shows a negative corralation);else use
the previous value. This script doesnt work,and to be honest
i dont think is the best solution to my problem. Any help
would be much appreciated.

vola = volume;
for i = 2:length(vola),
    if (vola(i) < vola(i-1)) && (vola (i+1) < vola (i)) &&
(vola (i+2) < vola (i+1)) || (vola(i) > vola(i-1))
        vola(i) = vola(i);
    else vola (i) = vola (i-1);
    end,
end

Subject: if statement troubles

From: Randy Poe

Date: 21 Feb, 2008 13:53:08

Message: 2 of 3

On Feb 21, 8:06 am, "Adam Bright" <abc_services_2...@yahoo.co.uk>
wrote:
> Hi there, I'm having some trouble trying to write an if
> statement that will work for my problem.
> Imagine there is data in the following way:
>
> 1;2;3;4;5;6;5;7;8;9;8;7;6;5;0;1;2;3
>
> I'm trying to write a statement which says that if the
> current value is less than the previous value, than to use
> the previous value instead. This will eridicate any anomiles
> in the data which I have. However there will be certain
> times when the data is actually decreasing and this needs to
> be the value used, however due to this being a resetting of
> an instrument, the volume hasnt actually decreased just the
> recording and the subsequent data will need to be added to
> the previous value prior to the value decreasing period.
> (e.g 1;2;3; added to 9) I've tried to make a script which
> states to use the current value if it is greater than
> the previous or that the three consequtive values are less
> than the one before (shows a negative corralation);else use
> the previous value. This script doesnt work,and to be honest
> i dont think is the best solution to my problem. Any help
> would be much appreciated.

We probably can't help you find the right algorithm,
but we can help you implement the algorithm you
have designed.

>
> vola = volume;
> for i = 2:length(vola),

Perhaps the order of comparisons isn't what you
intended. Try adding parentheses. As I understand it,
you want the first branch if either the first 3 conditions
are true, or the 4th condition is true.

Thus:
 New paren here--> if ( (vola(i) < vola(i-1)) &&
   (vola (i+1) < vola (i)) &&
    (vola (i+2) < vola (i+1)) ) <-- new paren here
        || (vola(i) > vola(i-1))

Or, slightly more readable:
   if all( vola[i i+1 i+2] < vola[i-1 i i+1] ) || vola(i) > vola(i-1)
                    ...

             - Randy

Subject: if statement troubles

From: Peter Boettcher

Date: 21 Feb, 2008 14:02:29

Message: 3 of 3

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

> Hi there, I'm having some trouble trying to write an if
> statement that will work for my problem.
> Imagine there is data in the following way:
>
> 1;2;3;4;5;6;5;7;8;9;8;7;6;5;0;1;2;3
>
> I'm trying to write a statement which says that if the
> current value is less than the previous value, than to use
> the previous value instead. This will eridicate any anomiles
> in the data which I have. However there will be certain
> times when the data is actually decreasing and this needs to
> be the value used, however due to this being a resetting of
> an instrument, the volume hasnt actually decreased just the
> recording and the subsequent data will need to be added to
> the previous value prior to the value decreasing period.
> (e.g 1;2;3; added to 9) I've tried to make a script which
> states to use the current value if it is greater than
> the previous or that the three consequtive values are less
> than the one before (shows a negative corralation);else use
> the previous value. This script doesnt work,and to be honest
> i dont think is the best solution to my problem. Any help
> would be much appreciated.

I can't follow your description. But check the precedence and the sense
of your boolean operators && and ||. "Ands" and "ors" at the same level
without parens is just asking for trouble. Is it (A&B) | C, or A &
(B|C)? Big difference. Just add parens to be clear about what you
want.

Also, you are the one in the best situation to debug your code. Try
generating some test vectors. For instance, run a version which tests
only the first of the conditions you mention. And instead of setting
your output, set a test vector. Use 1 if you want the current value,
and 0 if you want the previous. Now you can inspect the test vector
next to your data vector, and see if it is correct.

Then repeat for the second condition you want to test. Then put them
together. Finally, add back in the result generation.

If when it functions correctly you are not satisfied with the
performance, you can post it back here for some tips on vectorizing.
But correctly functioning code is the best way to specify your problem.

> vola = volume;
> for i = 2:length(vola),
> if (vola(i) < vola(i-1)) && (vola (i+1) < vola (i)) &&
> (vola (i+2) < vola (i+1)) || (vola(i) > vola(i-1))
> vola(i) = vola(i);
> else vola (i) = vola (i-1);
> end,
> end

-Peter

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
if statement logical operators Adam Bright 21 Feb, 2008 08:10:09
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

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.
Related Topics