{"id":696,"date":"2017-06-03T10:51:26","date_gmt":"2017-06-03T02:51:26","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=696"},"modified":"2023-01-28T11:41:41","modified_gmt":"2023-01-28T03:41:41","slug":"examples-on-using-the-sum-function-in-octave","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/examples-on-using-the-sum-function-in-octave\/","title":{"rendered":"Examples on using the sum function in Octave"},"content":{"rendered":"<p>If you are taking <a href=\"https:\/\/imp.i384100.net\/9WmaYE\" rel=\"noopener\" target=\"_blank\">Andrew Ng's Machine Learning course on Coursera<\/a>, the <code>sum<\/code> function is one of the functions that you will probably need to use to complete your assignments. Since the <code>sum<\/code> function can be counter intuitive in some cases, I document some of the examples that I had created while trying to get a better understanding of the <a href=\"https:\/\/www.gnu.org\/software\/octave\/doc\/v4.2.0\/Sums-and-Products.html\" target=\"_blank\" rel=\"noopener\">sum<\/a> function in Octave.<\/p>\n<h2>Using sum on a row vector in Octave<\/h2>\n<p>When the <code>sum<\/code> function encounters a row vector, it basically sums all the elements in the row vector and returns the total:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&gt;&gt; rowVector = &#x5B;1 2 3]\r\nrowVector =\r\n\r\n   1   2   3\r\n\r\n&gt;&gt; sum(rowVector)\r\nans =  6\r\n\r\n<\/pre>\n<h2>Using sum on a column vector in Octave<\/h2>\n<p>As with the case of using the <code>sum<\/code> function on a row vector, when the <code>sum<\/code> function encounters a column vector, it will add all the elements in the column vector and returns the total:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ncolVector = &#x5B;1; 2; 3]\r\ncolVector =\r\n\r\n   1\r\n   2\r\n   3\r\n\r\n&gt;&gt; sum(colVector)\r\nans =  6\r\n\r\n<\/pre>\n<h2>Using <code>sum<\/code> on a matrix in Octave<\/h2>\n<p>When a matrix is given to <code>sum<\/code> as a parameter, the behaviour can be counter intuitive at first. Although you may expect that the <code>sum<\/code> function returns a single value that is the total of adding all the elements in the matrix together, this is not the case.<\/p>\n<p>To see how the <code>sum<\/code> function handles matrix as an input, let's first define a 2 by 3 matrix:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&gt;&gt; x = &#x5B;1 2 3; 1 2 3]\r\nx =\r\n\r\n   1   2   3\r\n   1   2   3\r\n<\/pre>\n<h3>How to sum a matrix by column<\/h3>\n<p>Without any additional parameters to the matrix, the sum function collapses the matrix into a row vector by adding elements in the same column together:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&gt;&gt; sum(x)\r\nans =\r\n\r\n   2   4   6\r\n<\/pre>\n<p>This behaviour is similar to supplying a '1' as an additional parameter to the matrix:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&gt;&gt; sum(x, 1)\r\nans =\r\n\r\n   2   4   6\r\n\r\n<\/pre>\n<h3>How to sum a matrix by row<\/h3>\n<p>If a '2' is supplied with the matrix to the <code>sum<\/code> function, the <code>sum<\/code> function will take return a column vector that contains the subtotal of all items in each row added together: <\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&gt;&gt; sum(x, 2)\r\nans =\r\n\r\n   6\r\n   6\r\n\r\n<\/pre>\n<h3>How to sum all elements in the matrix<\/h3>\n<p>Summing all the elements in the matrix was what I had required in computing the cost function of my learning algorithm. There are three ways which I had tried: <\/p>\n<ol>\n<li>\nSumming the matrix by column and then summing the resultant row vector:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&gt;&gt; sum(sum(x , 1))\r\nans =  12\r\n<\/pre>\n<\/li>\n<li>\nSumming the matrix by row and then summing the resultant column vector:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&gt;&gt; sum(sum(x , 2))\r\nans =  12\r\n<\/pre>\n<\/li>\n<li>\nConverting the matrix to a column vector and then summing the resultant column vector:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&gt;&gt; sum(x(:))\r\nans =  12\r\n<\/pre>\n<\/li>\n<\/ol>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-be\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-be&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-be&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F696&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>If you are taking <a href=\"https:\/\/imp.i384100.net\/9WmaYE\" target=\"_blank\" rel=\"noopener\">Andrew Ng&#8217;s Machine Learning course on Coursera<\/a>, the <code>sum<\/code> function is one of the functions that you will probably need to use to complete your assignments. Since the <code>sum<\/code> function can be counter intuitive in some cases, I document some of the examples that I had created while trying to get a better understanding of the <a href=\"https:\/\/www.gnu.org\/software\/octave\/doc\/v4.2.0\/Sums-and-Products.html\" target=\"_blank\" rel=\"noopener\">sum<\/a> function in Octave.<\/p>\n","protected":false},"author":1,"featured_media":1269,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[416,414,419],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Octave-Logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-be","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/696"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=696"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/696\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1269"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}