Illustration of preallocating a vector, MATLAB in Engineering

Illustration of Preallocating a Vector:

Illustration of calling the function:

>> myveccumsum([5 9 4])

ans =

    5  14  18

At the first time in the loop, outvec will be [5]. Then in the second time, runsum will be 14 and outvec will store [5 14]. Lastly, runsum will be 18 and outvec will store [5 14 18].

Though this technique works, it is ineffective. The other method includes referring to each index in the output vector, and placing every partial sum into the next element in the output vector. Since each and every value of vec(i) is added to the running sum, this new sum is stored in the outvec(i).

1994_Illustration of Preallocating a Vector.png

Though initializing the output vector outvec to all zeros is not strictly essential, it greatly improves the efficiency of the function. Initializing this vector to all the zeros with similar size as the input argument preallocates that much memory for the outvec. Then, each element is changed in the loop to its right value. On the other hand, when building a vector up one element at a time, as in the earlier illustration, the MATLAB has to find a new memory each time the vector is extended.

The MATLAB has many other functions which work with vectors. 

Posted Date: 10/19/2012 3:39:18 AM | Location : United States







Related Discussions:- Illustration of preallocating a vector, Assignment Help, Ask Question on Illustration of preallocating a vector, Get Answer, Expert's Help, Illustration of preallocating a vector Discussions

Write discussion on Illustration of preallocating a vector
Your posts are moderated
Related Questions
num2str function: The num2str function, that converts real numbers, can be called in many ways. If only the real number is passed to the num2str function, it will generate a s

str2num function - String: The function str2num does the opposite; it takes the string in which a number is stored and converts it to the type double: >> num = str2num('123.

Algorithm for appex subfunction: The algorithm for appex subfunction is as shown:  Receives x & n as the input arguments.  Initializes a variable for running sum of t

Function fieldnames - structure functions: The function fieldnames will return the names of the fields which are contained in the structure variable. >> pack_fields = fiel

A matlab function to calculate filter order

Algorithm for the function e: The algorithm for the function eoption is as shown: Use the menu function to show the 4 choices. Error-check (an error would take place

Example of file ploting data: As the other example, a data file called 'compsales.dat' stores the sales figures (in millions) for divisions in a company. Each line in the f

Example to change the line width from the default: For illustration, to change the line width from the default of 0.5 to 1.5: >> set(hl,'LineWidth',1.5) As long as the

Reading from a Mat-File: The load function is used to read from various types of files. As with save function, by default the file will be supposed to be a MAT-file, and load

Uses of Function handles: The Function handles can also be generated for functions other than anonymous functions, both built-in & user-defined functions. For illustration, th