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 want to run 4 instances of my matlab code on 4 processor cores. im executing the job from head node. i created a parallel job and assigned number of workers. but i don''t get bac

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';

Execute a exponential function program: Running the script will take up the menu as shown in the figure: Then, what happens will totally depend on which button(s) the

Passing Structures to Functions: The whole structure can be passed to a function, or separate fields can be passed. For illustration, here are the two distinct versions of a f

Illustration of if - else statement: The one application of an if-else statement is to check for errors in the inputs to a script. For illustration, a former script prompted t

Symbolic Variables and expressions: The MATLAB has a type known as sym for the symbolic variables and expressions; these work with strings. The illustration, to generate a sym

Use polyval to evaluate the derivative at xder. This will be the % slope of the tangent line, "a" (general form of a line: y = ax + b). % 4. Calculate the intercept, b, of t

Passing arguments to functions: In all these functions examples faraway, at least one of the arguments was passed in the function call to be the value(s) of the equivalent inp

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

Comparing strings: There are few functions which compare strings and return logical true when they are equivalent or logical false when not. The function strcmp compares the s