• Register
Welcome to Kodlogs Q&A, programming questions and answer website.

Most popular tags

java program python php javascript android c# r spring c mysql eclipse arrays python-3 exception tomcat ggplot2 spring-boot numpy html sql-server jdbc ssl sockets windows string x python-2 hibernate 7 java-8 intellij-idea ssl-certificate entity-framework macos pandas minecraft ios csv security sql json asp list net class heap-memory for-loop scikit-learn winforms illegalstateexception keytool matlab redirect code program junit pip maven ubuntu loops math facebook datetime jquery cryptography loop int import https tsql fatal-error struts java-applet spring-mvc runtime-error apache-spark pyspark garbage-collection amazon-web-services sql-server-2005 quirks-mode internet-explorer-9 internet-explorer reporting-services anova machine-learning keras indentation-error unit-testing android-studio jupyter-notebook glmnet virtualenv webclient visual-studio-2010 webpack compiler-errors numpy-ndarray curl vagrant aspnet reporting dataframe nullpointerexception javafx tensorflow jsp django jpa docker wordpress servlet indentation xcode typeerror scanner sum deployment socketexception git linux certificate css util oracle cmd node recursion search tcp syntax connection float httpwebrequest js main function number file html5 spring-data-jpa softmax elastic-beanstalk google-sheets-api google-spreadsheet-api cnf minidump xuggler acumatica gitlab elasticsearch-5 elasticsearch app-transport-security ios10 ios9 cdn cloudflare wampserver plpgsql salt-stack sysv parent-child android-edittext textview visual-studio-2012 android-workmanager unique-index unity2d unity3d electron theano maven-compiler-plugin pickle assetbundle rstudio bar-chart python-tesseract tomcat6 primefaces solr easymock jvm-crash tomcat7 apache-httpclient-4 web-services woodstox discord jersey-2 prepared-statement resultset sencha-cmd sencha-touch-2 sencha-touch powermock fips apex x509certificate ibm-bpm websphere-7 file-permissions file-io accesscontrolexception grizzly atmosphere slick2d lwjgl informetica twitter-bootstrap-3 jax-rs resteasy spring-security-kerberos spring-security kerberos long-integer mapnik pytorch git-review vpython continuation homebrew xgboost conda visual-studio android-asynctask stack-trace user-interface jaxws-maven-plugin maven-3 maven-2 browser-notsupportedexception google-maps-api-3 google-maps visual-studio-code truststore firebase-authentication aws-organizations php-7 string-formatting cplusplus visual-studio-2015 net-core net-mvc msbuild extension-methods foreign-keys windows-services react-redux reactjs inputstream facebook-graph-api entity-framework-4 reportingservices-2005 mips linear-regression deep-learning block-device melt reshape2 floating-point webpack-dev-server javascrip 0-lollipop android-5 statsmodels avx eclipseide javafx-2 php-not-recognized laravel command-line tinyurl classnotfoundexception atom-editor android-emulator android-sdk-tools ionic2 cordova foobar2k tcplistener net-2 net-4 ole-db-provider export-to-excel openrowset windows-10 vagrantfile jvm-arguments global-variables orm autopostback classformatexception eof upstart qt ipv4 graphviz pydot atom curses unityeditor pandoc tesseract xls oledb redux spotify cxf wamp nio stacked prediction headless hyperlink outlook jackson keystore applicationcontext pipeline iis jstl encryption perfect-square objective-c carthage xcode8 indexing standard-deviation sudo xampp postgresql connector apple mdf kubernetes destructor gettime arithmeticexception gmail ioexception heuristics milliseconds disk cpu npm modx-revolution goldsky modx prevnext javascript-dictionary stack-smashing device-monitor radio-button android-actionbaractivity android-activity android-fragments java-long unqualified-id ora-12154 javc c++ java nullpointerexception runtime-error drjava awt-eventqueue dsx math-pow ajquery nosuchelementexception appcompatactivity jsf jtextfield awt jpanel inputmismatchexception glassfish deque jupyter flask lvalue gradle servlets netbeans simulation ibm factorial javax apache boot opengl virtualbox jvm margins 2147483647 mongodb cloud firebase plot plugins github processor automation crash card pointers editor concatenation formatting debugging devices module testing color arraylist sequence nested date caching expected build response ip directory algorithms release collections print figure casting url expression validation integer microsoft sorting sort read types replace rest ajax exe excel dictionary message required database text variable time size dll system files table runtime static random code web map http error version 0 2 my
0 votes
4 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?
by (8.9k points)  

1 Answer

0 votes

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 .

You might need to know what is the size of the matrix how many elements are in the matrix you might use that in your program so these are built-in functions MATLAB has I'm going to clear the screen put matrix 1b going clear screen again put matrix 1 back on the screen and let's do another one we'll call some and we use this in a couple sections ago I Ellis trated this for you but I'm going to show you one more time when you apply the sum operator to a vector just a regular old vector which is a list of numbers the MATLAB adds up everything in that vector and gives you the answer when you apply the sum to a matrix what MATLAB does is it adds up the columns so when you sum these three things together the three the one and the negative one you get three four plus one is five five plus one plus two is eight and so on so it's summing up the columns right and that's what it's giving you whenever you whenever you're whenever you apply the sum operator to a matrix because it's not going to default to adding every element up it defaults to adding all the columns together if you want to really add up everything in the matrix then you can just do it twice and what happens is the inner guy gets evaluated first and this is going to produce this little vector of numbers which is the sum of it all the columns and then when you apply the sum to this vector that this returns it adds up everything everything it basically adds up all of these numbers and so when you do that you get 16 so when you add all of these things together all of these elements you get a number of 16 all right let me clear the screen put the matrix back on the screen one more time and I'm going to show you a couple of functions that we use back with vectors and show you kind of how they behave with matrices we showed you how to use the min function in vectors like if you just had a listing of numbers a vector then when you find the men it's just going to give you the lowest value the least value of all of the vector values in your vector when you apply it to a matrix it doesn't give you the smallest value of everything here it does it column by column so it looks in column one and it says the smallest values negative one column two smallest value is zero column three and smallest value is 1 and so on smallest value here is negative 3 when you apply some of these things that work intuitively it's vectors when you apply two matrices MATLAB in general likes to work column by column to give you that and the same thing happens if you use the max function when you want to find the maximum value of whatever is enclosed in your matrix it goes column by column here's three here's four here's five here's two all right now if you really wanted to know now let me clear the way I'll leave it up here if you really wanted to know what is the minimum value in your entire matrix then you can just do it twice min min matrix 1 make sure you understand that what's happening here is first MATLAB goes in the middle it evaluates min of matrix 1 you basically get this vector back and then it applies the min function again to what you get back and you can see the smallest value here is negative 3 and so you get negative 3 so this is the smallest value of the entire matrix so if you have a matrix of some survey respondents with a thousand you know people or something like that then you could easily find the minimum value you know without poring through a spreadsheet or whatever and you can you can do the same thing with max just apply it twice and the maximum number and contained in this matrix as far as elements go it's 5 so that's not really all I wanted to show you in this section just some general little functions to learn more about the matrix to prove it you can find the size of the matrix which is basically the dimensions the length of the matrix which is basically which is the largest dimension the number of elements it can calculate for you the sum of the elements.

 How to find the minimum in the maximum values of what's contained in your matrix so when you're typing in small matrices like this it's not necessarily helpful but you also have to consider that even if it's a small matrix I mean you know I'm teaching you all this as far as fundamentals in terms of okay we're typing in matrix 1 here is what it is and then we're applying some stuff to it but you might consider that you might have a even if you don't talk about importing data you might have a function or a you know a dot M file that we'll learn how to create later that's going to go off and you know you might have some some input values and it's doing a long calculation some simulation or something and the result of the simulation is a matrix the matrix might only have so twelve or sixteen elements it may not be huge but the point is the matrix is going to be calculated you know each time you run the program so it's going to be a little different so you might have a need to calculate this matrix and then pull the maximum value out of it you know or to pull the minimum value out of that matrix automatically and display that on the screen and each time you do the calculation you might get a different matrix for an answer and so a different minimum value or different maximum value so some of these functions that seem a little bit hokey they just seem hokey because we're typing the matrix in but you need to think about okay what happens if I'm running a program that's calculating a matrix answer and then I want to pull values out of that or I want to learn about what the maximums are or whatever so these functions are built into MATLAB so make sure you know how to use them and play around with and they're real simple to use and you'll see exactly how MATLAB is trying to work here 

by (8.9k points)  
...