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
Referring to and Showing Cell Array Elements and Attributes: Just as with the other vectors, we can refer to individual elements of the cell arrays. The only difference is tha

ischar function: The ischar function return the logical true if an array is a character array, or logical false if not. >> vec = 'EK127'; >> ischar(vec) ans =

Defined a variable in work space: The variables defined in the script will become a part of the workspace: >> clear >> who >> mysummfile    15 >> who

Patch function - graphics objects: The patch function is used to generate a patch graphics object, which is made from 2-dimensional polygons. The patch is defined by its verti

Splits a string : The strtok function splits a string into pieces; it can be called in many ways. The function receives one string as an input argument. It appears for the fir

Matrix solutions to systems of the linear algebraic equations: The linear algebraic equation is an equation of the form a 1 x 1 + a 2 x 2 + a 3 x 3    .  .  .  .  a n x n

Technique is to create one element - vector: Technique is to create one element with the values from one structure, and use repmat to replicate it to the preferred size. Then,

Data structures: The Data structures are variables which store more than one value. In order to made sense to store more than one value in a variable, the values must in some

Use of built-in colormaps: MATLAB has built-in colormaps, it is also possible to generate others by using combinations of any colors. For illustration, the following generates

Changing Case: The MATLAB has two functions which convert strings to all uppercase letters, or all lowercase, known as the upper and lower. >> mystring = 'AbCDEfgh';