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

• Feb 20th 2010, 03:09 AM
Mathhelpz
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:

http://img23.imageshack.us/img23/2995/44567558.png

%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]
• Feb 20th 2010, 05:20 AM
elbarto
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
• Feb 20th 2010, 07:45 AM
Mathhelpz
hmm alright, thanks ill post back if i run into any problems =D
• Feb 20th 2010, 06:18 PM
Mathhelpz
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
• Feb 21st 2010, 02:24 AM
elbarto
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
• Feb 27th 2010, 02:30 AM
Predictor
Quote:

Originally Posted by elbarto
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