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
Illustration of Sound files: For illustration, the following script generates a subplot which shows the signals from chirp and from train, which is as shown in figure:

Gauss Elimination: The Gauss elimination technique consists of:    Generating the augmented matrix [A b]    Applying EROs to augmented matrix to obtain an upper trian

analyzing traffic; determine motion of flow; calculate tracklets; detect abnormalities;

Example Exit modular program: In the illustration below, the user  Chose the Limit; -   Whenever prompted for n, entered the two invalid values before finally ente

Illustration of Sorting strings: To sort on the rows rather than second dimension should be specified. >> sort(words,2) ans = Hello Hdowy Hi Gbdeo

Illustration of Gauss elimination: For illustration, for a 2 × 2 system, an augmented matrix be: Then, the EROs is applied to obtain the augmented matrix into an upper

IS Functions in Matlab: There are many functions which are built into MATLAB which test whether or not something is true; these function names start with the word is. As these

Creating the structure Variables: Creating a structure variable can be accomplished by simply storing the values in fields by using assignment statements, or by using the stru

Cross Product: The cross or outer product a × b of two vectors a and b is defined only whenever both a and b are the vectors in three-dimensional space, that means that they b

IS Functions for Strings: There are many functions for strings, that return logical true or false. The function isletter returns the logical true when the character is a lette