Results 1 to 6 of 6

Math Help - Matlab Split vectors,if statements, and "for loop"

  1. #1
    Junior Member
    Joined
    Feb 2009
    Posts
    29

    Matlab Split vectors,if statements, and "for loop"

    Don't really know where to begin but here's what information they give and what i have so far. I guess what my question is, is what am i suppose to do and how am i suppose to do it. It says i need to use iteration, so that means "for x=" then if->else if>else if->else-> end" format right? SO could someone help me start it

    Separating a vector into 6 subsets:



    %Use the following vector given for Matlab:
    D = [ 23, -5, 4, 0, -10, 45, 3, -3, 34, -9, 1, 25, 87, 21, -7, 4, -2, 6, 62, -22, -6, -17]

    posEven = [4,34,4,6,62]

    negEven = [-10,-2,-22,-6]

    posodd = [23,45,25,87,21]

    negodd = [-5,-3,-9,-7,-17]

    posNeg = [1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0]

    big = [45, 34, 87, 62]





    Example given to us:

    If D = [-4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 34, 87],
    posEven=[2,4,34]
    negEven=[-4,-2]
    posOdd=[l,3,5,87]
    negodd=[-3,-1]
    posNeg = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1] and
    big = [34, 87]

    Use the following vector (copy and paste in Matlab):

    D = [ 23, -5, 4, 0, -10, 45, 3, -3, 34, -9, 1, 25, 87, 21, -7, 4, -2, 6, 62, -22, -6, -17]
    Last edited by Mathhelpz; February 20th 2010 at 06:18 AM.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    Here is an example of how to get started with the first problem. Search for the function names in the help files if you are not familiar with any of them - they are explained very well.

    Code:
    function out =  posEven(v)
    out = [];
    for i = 1:length(v)
        if v(i) > 0
            if mod(v(i),2)==0
                out = [out v(i)];
            end
        end
    end
    Example:
    Code:
    EDU>> D = [ 23, -5, 4, 0, -10, 45, 3, -3, 34, -9, 1, 25, 87, 21, -7, 4, -2, 6, 62, -22, -6, -17]
    
    D =
    
      Columns 1 through 16
    
        23    -5     4     0   -10    45     3    -3    34    -9     1    25    87    21    -7     4
    
      Columns 17 through 22
    
        -2     6    62   -22    -6   -17
    
    EDU>> posEven(D)
    
    ans =
    
         4    34     4     6    62
    Have a go at coding some of the rest of the functions. Once you learn how to loop through an array and test variables like above then you will find you can do a lot of really helpful things quite easily.

    Regards Elbarto
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Junior Member
    Joined
    Feb 2009
    Posts
    29
    hmm alright, thanks ill post back if i run into any problems =D
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Junior Member
    Joined
    Feb 2009
    Posts
    29
    still stuck..

    It should all be in one script right...I wanted to do
    for j = (:,k)
    if
    elseif
    elseif
    else

    however i still can't find a way to make a script for this problem
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    what are you trying to achieve with this line?

    Code:
    for j = (:,k)
    Post up your attempt (use code tags - the # button on the post toolbar) and you might get a some more problem specific help.

    Elbarto
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Newbie
    Joined
    Mar 2009
    Posts
    12
    Quote Originally Posted by elbarto View Post
    Here is an example of how to get started with the first problem. Search for the function names in the help files if you are not familiar with any of them - they are explained very well.

    Code:
    function out =  posEven(v)
    out = [];
    for i = 1:length(v)
        if v(i) > 0
            if mod(v(i),2)==0
                out = [out v(i)];
            end
        end
    end
    I know the instructions specify if..then statements, but just for reference, here is the vectorized version of the above (without using a function):

    Code:
    posEven = D( (D > 0) && (mod(D,2) == 0) );
    Example:

    Code:
    >> D = [ 23, -5, 4, 0, -10, 45, 3, -3, 34, -9, 1, 25, 87, 21, -7, 4, -2, 6, 62, -22, -6, -17]
    
    D =
    
        23    -5     4     0   -10    45     3    -3    34    -9     1    25    87    21    -7     4    -2     6    62   -22    -6   -17
    
    >> posEven = D( (D > 0) & (mod(D,2) == 0) )
    
    posEven =
    
         4    34     4     6    62
    -Will Dwinnell
    Data Mining in MATLAB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 2
    Last Post: April 24th 2011, 08:01 AM
  2. Replies: 1
    Last Post: October 25th 2010, 05:45 AM
  3. Replies: 1
    Last Post: June 4th 2010, 11:26 PM
  4. Problem Transitioning Equations With "if" Statements
    Posted in the Math Software Forum
    Replies: 8
    Last Post: January 20th 2010, 10:46 AM
  5. Replies: 23
    Last Post: September 8th 2006, 01:05 PM

Search Tags


/mathhelpforum @mathhelpforum