{"id":4393,"date":"2017-09-23T04:57:50","date_gmt":"2017-09-23T04:57:50","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=4393"},"modified":"2021-03-20T14:15:19","modified_gmt":"2021-03-20T14:15:19","slug":"how-to-iterate-over-a-vector-in-octave-script","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/how-to-iterate-over-a-vector-in-octave-script\/","title":{"rendered":"How to iterate over a vector in Octave Script?"},"content":{"rendered":"<h3>Introduction<\/h3>\n<p>In this tutorial, let&#8217;s learn how to iterate over a vector in the Octave script. We will write an octave script to compute the average of numbers in a vector. We will iterate the vector using a for a loop.<\/p>\n<h3>Octave Script<\/h3>\n<pre>function Average = IterateOverVector (numbers)\r\n        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r\n        % Given the vector of numbers\r\n        %   Inputs:\r\n        %       numbers vector\r\n        %   Outputs:\r\n        %       Average of numbers\r\n        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r\n\r\n        count=0;\r\n        sumOfNumbers=0;\r\n        %Iterate over the vector using for loop\r\n        for i = 1:numel(numbers)\r\n        count = count + 1;\r\n        sumOfNumbers = sumOfNumbers + numbers(i);\r\n        end\r\n        if count == 0\r\n        Average = 0;\r\n        else\r\n        Average = sumOfNumbers\/count;\r\n        end\r\n        endfunction\r\n\r\n\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4409\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Octave-Script.png\" alt=\"Octave Script\" width=\"1589\" height=\"761\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Octave-Script.png 1589w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Octave-Script-300x144.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Octave-Script-1024x490.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Octave-Script-768x368.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Octave-Script-1536x736.png 1536w\" sizes=\"auto, (max-width: 1589px) 100vw, 1589px\" \/><\/p>\n<p><strong>numel()<\/strong> is the function that returns the number of elements in the provided vector as an argument.<\/p>\n<h3>Driver<\/h3>\n<p>Sample driver script to call the Octave function.<\/p>\n<p>&nbsp;<\/p>\n<pre>% Sample Driver script \r\nnumbers=[ 2 6 77 56 94 35 ] \r\naverage= IterateOverVector(numbers); \r\nfprintf('Average = %10.4f \\n', average);<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-4393\" data-postid=\"4393\" class=\"themify_builder_content themify_builder_content-4393 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Introduction In this tutorial, let&#8217;s learn how to iterate over a vector in the Octave script. We will write an octave script to compute the average of numbers in a vector. We will iterate the vector using a for a loop. Octave Script function Average = IterateOverVector (numbers) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Given the vector of numbers [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[],"class_list":["post-4393","post","type-post","status-publish","format-standard","hentry","category-octave","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/4393","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=4393"}],"version-history":[{"count":5,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/4393\/revisions"}],"predecessor-version":[{"id":7820,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/4393\/revisions\/7820"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=4393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=4393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=4393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}