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
Representing Logical true and False: It has been stated that expressions which are theoretically true really have the integer value of 1, and expressions which are theoretical

Example of Functions which return values: Calling this function prints the circumference, since there is no way to store the value, hence, it can be used in following computat

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 customizing plots: As the other illustration of customizing plots, the pieces of a pie chart can be "exploded" from the rest. In this situation, the two vectors are

function sortrows - sorting strings: The function sortrows sorts each and every row as a block, or group, and it also will work on numbers. Here in this illustration the rows

Vectors of nested structures: The Combining vectors and nested structures are possible to have a vector of structures in which several fields are structures themselves. Here i

Example of Nested functions: For illustration, the function below computes and returns the volume of a cube. Three arguments are then passed to it, for the width and length of

Median The median is defined only for a data set which has been sorted first, that means that the values are in order. The median of a sorted set of data values (n) is defined

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

Example of Vectorizing: Likewise, for an operation on a matrix, a nested loop would be needed; for illustration, supposing a matrix variable mat: [r c] = size(mat); for