How to Increase a Vector in MATLAB
MATLAB is a technical programming platform used for analysis and simulation that is optimized for operations on vectors and matrices. However, using matrices and vectors to encapsulate mathematical operations complicates memory management and mathematical operations. Built-in MATLAB functions and proper programming practices allow you to use MATLAB's matrix optimization to the fullest.
Instructions
-
-
1
Preallocate vectors and matrices to simplify memory management. Whenever the extent of a vector is increased, MATLAB must find a new chunk of memory to contain the data. For example, if you are reading in 25 data trials, the maximum length of which contains 200 samples, preallocate an empty array of extent 25 by 200.
my_array = zeros(25,200);
-
2
Perform element-wise operations on array by using the "." modifier to the mathematical operations. For example, add the value 2.5 to each element of an array using the ".+" operator.
my_increased_array = my_array .+ 2.5;
my_doubled_array = my_array .* 2.0; -
-
3
Increase the extent of an existing vector by creating a new vector and transferring the old vector into it. Although MATLAB will increase the extent of a vector by addressing and index out of bounds, it can result in poor memory performance.
my_array = rand(50,50);
my_new_array = zeros(100,100);
my_new_array(1:50,1:50) = my_array;
clear my_array;
-
1
References
- Photo Credit Ablestock.com/AbleStock.com/Getty Images