1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab matrix selection of data using for loops and if statements ,,

  1. Apr 7, 2009 #1
    matlab matrix selection of data using for loops and if statements,,,,,

    Hi people I was wondering if someone could assist me with the following;

    You received the following data values from a coworker:
    data = [-1.2, 3.3, 5.1, 0.2, 2.9, -0.1];

    It turns out that the sensor used to collect the data is prone to noise and the real data should only contain positive even numbers. Write an M-File that rounds the positive values in data to the nearest EVEN integer. For example, 2.5 should be rounded to 2 and 3.1 will be rounded to 4. Negative numbers should be set to 0. HINT: You may want to use if statements and for loops

    I can't do this question .... I have having issues with programming and am willing to put in the work to get really good.

    My overall pattern of thinking was;

    1) Fist of all I know that I want to select the positive answers and I will have to work with either mod/rem to get my remainders. Since I will have to differ between neg & positive I choose rem.

    x = rem(data,2) % finds the positive and negetive remainder of the data matrix.

    x =

    -1.2000 1.3000 1.2000 0.2000 0.9000 -0.1000

    2) Ok ... so I need to get rid of the negs....
    but if I go x > 0 then I will just get
    0 1 1 1 1 0
    and the values of my remainder are gone

    I am really lost with this questions and it is meant to be 'easy' .... sigh .... I have played around for almost 3 HOURS and cannot get it... Please help me....

  2. jcsd
  3. Apr 7, 2009 #2
    Re: matlab matrix selection of data using for loops and if statements,,,,,

    So you have the groundwork laid out for one possible solution - keep going along this path and you'll get there soon.

    First, a comment about:

    What is happening here is your x vector is being tested against 0 element-wise. Each element is either greater than 0 (denoted by 1, also known as true) or not (denoted by 0, also known as false). So basically, MATLAB is telling you, for each element, if it is greater than 0 or not. (Now the output should make sense.)

    Going forward for a first implementation, you should take the hint given in the problem: use a for loop and if statements. Basically, you need to loop through each element of your input vector and perform an "if" check. Then there are two cases denoted by 1 (true) or 0 (false). Based off if x is greater than 0 or not, you can do what you need to in each case. Note that you should probably try to create a simple loop and process each element in the vector independently (vector elements can be accessed by x(i)).

    Hopefully the comment about why you are getting the 0s and 1s in your point number 2 will help move you along some more.
  4. Apr 8, 2009 #3
    Re: matlab matrix selection of data using for loops and if statements,,,,,

    Hi I am still nowhere with this question;;;; GRRR so frustrating!!!

    I understand that;
    1) I need to use rem(data,2) to find the remainder and that;
    if the remainder is positive and the value of it is greater then 1 then I must take the ceil(data)
    2) If rem(data,2) is less then 1 but greater then 0 I must take a floor(data)
    3) If rem(data,2) is negetive that data value must be 0.

    I CANT put this into a code. I just don't understand how to do this in matlab I have been trying for 2 days now.... can someone please help me? Thankyou so much!
  5. Apr 9, 2009 #4
    Re: matlab matrix selection of data using for loops and if statements,,,,,

    You just need to put it all inside of a case statement.

    so after you run rem, you have another vector, which you called "x" above

    for r=1:length(x)
    if x(r)<0
    elseif x(r)>0 && x(r)<1

    sorry its kind of hard to see, I dont know how to indent.

    If "x" is run through that statement, it should modify it to your specifications above (assuming I understood what you were saying there ;) ). I hope this helps.
    Last edited: Apr 10, 2009
  6. Jul 28, 2009 #5
    Re: matlab matrix selection of data using for loops and if statements,,,,,

    I know it's too late to help the questioner with their homework, but I found this question in a web search and felt compelled to comment... I'm sorry, but the instructor should be flogged using something substantially firmer than a wet noodle. matlab is MATrix LABratory - it is optimized to deal with matricies, so use matrices and vectors whenever possible, not for loops with if statements - that's the domain of C and FORTRAN. there are times to use for loops and if statements in matlab, but this is most emphatically not one of them. I just got done taking a doubly nested for loop (indexing into a matrix) out of some one else's subroutine and sped it up from taking 120 seconds to execute down to just under 3 seconds (2.78 seconds, actually). just a slight improvement in execution time (over 40x or 1.6 orders of magnitude!).

    the programming clues in the program assignment are:
    - eliminate negative values
    - round to even numbers (hard to do, unless you think first)

    data=[-1.2, 3.3, 5.1, 0.2, 2.9, -0.1]

    mask = (data>=0) % 0 for negative, 1 for non-negative (0 and positives)
    limited = mask.*data % positive values unchanged, negatives are now zero
    rounded = 2*round(limited/2) % round to even numbers

    rounded = 0 4 6 0 2 0

    it really is that easy, and I've only been using matlab for a few weeks. think first, then code. trust me, you'll like the results much better. and use sensible variable names and comment your code - you'll appreciate it when you go back to your code after a hard Friday night... 8) don't ask me how I know... 8)

    oh, and here's a one line version... rounded = 2*round(((data>=0).*data)/2)
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Matlab matrix selection of data using for loops and if statements ,,
  1. For loops in Matlab (Replies: 22)

  2. MatLab for loops (Replies: 3)