Need of a nested loop, MATLAB in Statistics

Need of a nested loop:

How would we sum each individual column, rather than getting an overall sum?

Answer:

The programming technique would need a nested loop in which the outer loop is over the columns. For illustration, we will modify the earlier function to sum each column and return a row vector containing the results.

408_Need of a nested loop.png

2287_Need of a nested loop1.png

Note: The output argument will be a row vector containing similar number of columns as the input argument matrix

Also, as the function is computing a sum for each column the runsum variable should be initialized to 0 for every column. Therefore it is initialized inside of the outer loop.

 

>> mat = [3:5; 2 5 7]

mat =

    3   4   5

    2   5   7

 

>> matcolsum(mat)

ans =

    5   9   12

Obviously, the built-in sum function in a MATLAB would accomplish similar thing.

Posted Date: 10/19/2012 3:49:36 AM | Location : United States







Related Discussions:- Need of a nested loop, Assignment Help, Ask Question on Need of a nested loop, Get Answer, Expert's Help, Need of a nested loop Discussions

Write discussion on Need of a nested loop
Your posts are moderated
Related Questions
Advanced File Input and Output: In that section, we saw how to read the values entered by user using the input as well as the output functions disp and fprintf, that shows inf

Persistent Variables: Generally, whenever a function stops executing, then the local variables from that function are cleared. That means that each and every time a function i

Areacirc function: The areacirc function can be called from the Command Window as shown here, or from a script. Here is a script which will prompt the user for the radius of o

Combining Nested FOR Loops and the IF Statements: The statements inside a nested loop can be any of the valid statement, involving any selection statement. For e.g., there can

Indexing: The Indexing is an alternate to sort a vector. With indexing, the vector is left in its original order. An index vector is used to point values in the original vecto

Example of Core objects: The one core graphics object is a line that is also what the plot function produces. Here is an illustration of generating a line object, altering som

Function polyval - interpolation: The function polyval can then be used to compute the polynomial at particular values. For illustration, we could compute at every value in th

Illustration of Customizing plots: Illustration, the bar and barh functions by the default place a width of 0.8 between bars. Whenever called as bar(x,y), the width of 0.8 is

Illustration of Nested loops: Running the script shows the output: >> printstars ***** ***** ***** The variable rows identifies the number of rows to print, and

Rectangle - graphics objects: The other core graphics object is the rectangle that can have curvature added to it (!!). Merely calling the function rectangle without any argum