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
I have a 400x2 vel.dat and 20x2 xy.dat file, how can i plot a streamslice graph on matlab with this two files.

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

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

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

Illustration of Passing arguments to functions: Here is an illustration of calling this function: >> printrand() The random # is 0.94 As nothing is passed to

Preallocating a Vector: There are necessarily two programming techniques that can be used to simulate the cumsum function. One technique is to begin with an empty vector and c

Illustration of finding a sting: Let's enlarge this, and write a script which creates a vector of strings which are phrases. The outcome is not suppressed so that the string

Logical scalar values: The MATLAB also has or and and operators which work element wise for the matrices: These operators will compare any of the two vectors or matric

Vectors of Structures: In numerous applications, involving database applications, information generally would be stored in the vector of structures, instead of in individual s

Binary Search: The binary search supposes that the vector has been sorted first. The algorithm is just similar to the way it works whenever looking for a name in a phone direc