# sum matlab

17 views
How do you sum in Matlab? What is SUM function in Matlab? How do I find the sum of a matrix? How do you add two matrices in Matlab?

In this section of the MATLAB tutor here in this section we're going to apply some built-in functions that MATLAB has two matrices so we're a little bit more about them so specifically we'll be using the size operator and the length and and min and Max and things like that to learn more about matrices and also keep in mind we've done most of these functions and apply them the vectors already so this is sort of a natural extension of what we've already done so what we have here is let's go and create a matrix we'll call it matrix one and let's go ahead and just put some elements in there this could be anything but let's say 3 4 5 2 and then for the next row we'll 1 1 1 1 and for the next row you know we'll do negative 1 0 2 negative 3 and I think that's going to probably do it for what we need so let's go ahead and define that matrix right there so here we have a matrix we have 3 rows and we have 4 columns right all right.

So the first thing is we have this guy defined here what is going to happen if we use the command size there is a command in MATLAB called size and you can apply it to vectors and matrices so let's supply let's supply it to to this matrix here matrix 1 what do you think is going to come back with well MATLAB is going to tell you it has 3 rows and 4 columns so that's exactly what we have here so notice notice MATLAB returned two numbers but more than that MATLAB returned a vector I'm trying to point out to you that the way MATLAB works is when it ruin it needs to return more than one value for anything usually it arranges it in the form of a vector anytime.

You see the number spaced apart horizontally like that that's an internal representation of a vector in fact you can see the last answer over here bracket 3 comma 4 that is the that is the representation of a vector if I had just created one that's exactly what it would look like so matlab's coming back and saying okay there's two relevant numbers here one of those three one of them's four and that's the size so you don't get one number back you just get the dimensions basically and you know that's you know that's useful actually because even though we type this matrix in you might have a situation where you need to kind of expand your mind and yeah we can see three rows and four columns here but if you have data that you're importing into MATLAB large data sets you may not know ahead of time how many elements are in the rows and the columns so if you import everything in then this might be something that you might write into your MATLAB program to tell you how how large it is all right another relevant command to this guy would be length and we use length when we applied it to vectors you can see matlab's telling you to to stick an argument in there for an array so we'll just put this matrix in here for length.

What do you think that's going to put so we'll put that answer in and you get a fork and you scratch your head a little bit you're like how is the length of this for right well what's happening here is the size of the matrix was returned as as a three rows and four columns the length of a matrix is always going to be the larger of these two numbers so if this were three rows and five columns it would it would return a five if this were 16 rows and two columns it would return 16 so whichever one is the largest value of of the rows and the columns of the dimensions of the matrix is what's what it's going to return for the length and we've already applied this length operator to vectors and that just gives you the number of elements in the vector but when you have two dimensions MATLAB has to choose what to tell you the length of the thing is and it's just going to pick the largest number is that very useful for you not sure but it could be useful if you're importing large data sites now this is a I think a little more useful number of elements num L like that number of elements that's what that stands for and you can pass it matrix well look at that I didn't I didn't define the matrix properly matrix 1 that's the name of our matrix 12 and this is exactly what you would think it is it's the number of elements contained in this matrix so in other words we have three rows and four columns so we all know from multiplication three times four is twelve there's twelve elements in this matrix so MATLAB is calculating that for you and in fact that is useful as well again if you're pulling in a large data set from some survey or some experiment .