Matlab Basics: Parsing Vectors in For Loops
I have a vector of a specific length that is not predetermined, and I am trying to create a new vector of only numbers divisible by 2. This must be done using for loops to parse the vector.
I am going to focus on the algorithm and syntax of the for loops rather than the setup of the function itself.
So… where do I start? Let’s start with a general plan or outline of what I want to achieve.
I need to be able to access every number in the vector individually.
Then each number needs to be checked to see if it’s divisible by 2.
That number then needs to be placed into a new vector.
How do I check every number individually? Thats where parsing the vector using for loops come into play. Parsing the vector just means to analyze it by its individual components which is what I want.
Using for loops lets me access each element in the array easily. This allows me to compare and perform actions on each number individually.
The general syntax for a for-loop is as follows:
I use an iterator, in this case I assign it to the variable “i”. It is important to note that this variable changes after every iteration in the for loop, and I can specify how it changes after every loop. For example:
To integrate this with my vector I first need to find the length or dimensions.
Recall how you specify a location in an array: vec(r, c).
Since this is a row vector I know that r = 1 always and consequently that the columns are what will be changing in the for-loop.
I can now assign i from the loop to c: vec(1,i)
If I put this into the loop and run it I will find that it will traverse the vector and read every number individually:
Now that I figured out how to access each individual number in the array I have to compare each one to see if it is divisible by 2.
Let’s use an if statement in the for loop in conjunction with the previous expression.
If this statement is true I know the number in the ith position is divisible by two.
Now, I want to insert this number into a new vector.
When I do this I cannot simply assign vec(i,1) to a new variable (I will call it newVec).
If is said newVec = vec(i,1), the value of ‘newVec’ would change every iteration and I would be left with the last number in ‘vec’ that is divisible by 2.
What I need to do is specify in what location of ‘newVec’ I want to insert the number in. Naturally the first number that is divisible by two will go in the first column of ‘newVec’, the second number divisible by two will go in the second column of ‘newVec’ and so on and so forth.
Since I only want to store a value if it passes the if statement, the number of columns in ‘newVec’ is not dependent on i. It is dependent on how many times it passes an if statement. To keep track of this lets use a counter variable ‘count’. I can initialize it outside of the loop so it starts at the first spot in ‘newVec’.
Once I reach the if statement and assign ‘newVec’ the value from ‘vec’. I need to increment count so next time it will place the number in the next location of the array rather than override the previous number.
If I start with a vec with the following values:
vec = [2 5 6 9 2 11 20 31 7 8];
The final result is a new vector with only values that are divisible by 2.