MATLAB - Loops for Vectors and Matrices

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] all right so moving on to the context of vectors and matrices we'll see how we can use loops to access individual elements in vectors first in this demonstration i'm using a 1 by Phi row vector and a Phi by 1 column vector so whenever you know the size of you know the vectors that you're dealing with it's better to pre-allocate them okay and the indexing is like you know it starts with 1 and ends with Phi so this is a sample code as to how to access individual elements in these vectors so basically I'm replacing every element in the vector with its index a single looping variable is sufficient to deal with row vectors or column vectors whereas matrices they have both rows and columns that's why we would need both I and J there are 2 different variables to access individual elements in a matrix so we have seen this earlier in the semester basically the variable I is used for rows J for columns and a of I comma J is the way to access matrix elements now let's look at this task of creating a five by five matrix full of 50s using for loops if you look at the program you are pre allocating the matrix size to you know a 5 by 5 with full of zeros and then you're starting out with for I equals to 1 colon 5 and J equals to 1 column 5 now first the value of I is going to be 1 that's when it enters the next statement the value of J is going to be 1 and then it enters the action which is matte of 1 comma 1 equals 250 ok so the next step is the value of J is going to be 2 and then matte of 1 comma 2 is going to be changed to 50 and this repeats so it's repeating for J equal to four and five as well the entire first row is done executing so this is when it will switch back to the first four loop of I equals to two which is the next value in line and it goes on and on so this is the result when we are halfway through and one thing if you observe these operations are being performed row wise okay so this is the final result once this code finishes executing and all the operations are performed row wise now if I swap this I and J you know I if I use J first rather than I you would see that these operations are going to happen column wise that's because you're fixing the J and then varying the value in I okay so now I equals to two followed by I equals to three and then I equals to four and I equals to five I'll let you observe these operations very carefully feel free to like rewind this video and get a full understanding of how the operations are being performed row wise or column wise when we swap the variables I and J so now that we established that you know the variable I is used for rows and J is used for columns let's try to solve this problem okay so you need to create a matrix of five fi matrix with this pattern so the first step is to identify the pattern if you see the values are increasing like row wise and it's sort of a running sum okay so 1 1 plus 1 is 2 2 plus 1 is 3 and so on now let us write a MATLAB script to do this using loops the first step is to pre-allocate the matrix that we are trying to create with zeros okay so this pre allocation step is very important the next step is to create a running sum variable we start out with the value 1 because that's the value in the first element okay now like I said we are going to do it row wise so we start out with I equals to 1 colon 5 and then for J equals to 1 colon 5 and then we'll say mat of I comma J equals to run underscore some okay so that makes your mat of 1 comma 1 has the value of 1 and then you will have to increment this run some variable by a value of 1 each time when it enters this loop so I've written that code I'm closing those loops with the end statements and finally I'm going to display the you know the result so let me run this and see if we are getting the same value there you go so we've but is this the only way to create this the answer is no we could use vectorize code okay so the vectorized code implementation for this task looks like this it's reshape 1 : 25 and 5 by 5 with the transpose that is going to create exactly the same matrix that we started out with all right so wherever possible use MATLAB vectorized code it is possible that the vector score does not exist in MATLAB so that is when you will have to use the loops to you know get what you want so that way you can write MATLAB functions on your own okay [Music] in class lectures see you soon
Info
Channel: Radhakrishna Dasari
Views: 3,038
Rating: 4.9310346 out of 5
Keywords: Programming, MATLAB
Id: xcAm-9beBXw
Channel Id: undefined
Length: 6min 10sec (370 seconds)
Published: Thu Oct 03 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.