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
Logical Built-In Functions: There are built-in functions in the MATLAB which are useful in conjunction with vectors or matrices of all logical true or false values; two of the

Nested Functions: We have seen that the loops can be nested, that means that one inside of the other, functions can be nested. The terminology for nested functions is that an

Example of sorting strings: To sort a cell array of strings, also the sort function is used. The illustration is as follows, >> engcellnames = {'Chemical','Mechanical',.

Curve Fitting: The data is fundamentally either discrete or continuous. In most of the applications, data is sampled, for illustration, The temperature recorded every ho

Function Stubs: The other common debugging method, which is used when there is a script main program which calls many functions, is to use the function stubs. A function stub

Generic code for Reading from Files: The generic code to complete this is as shown below:   fid = fopen('filename'); if fid == -1    disp('File open not suc

Illustration of Median When the vector is not in sorted order to start with, the median function will still return the right answer (it will sort the vector automatically). For

Illustration of nested for loops and the if statements: For illustration,when the file contains: 33   -11  2  4      5   9 22     5  -7  2     11    3 the outco

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

Vectorizing: In most of the cases in MATLAB, loops are not essential. As MATLAB is written specifically to work with the vectors and matrices, most operations can be completed