How to Initialize an Array in MATLAB
To initialize an array in MATLAB, we can use one of the following methods:
- Using the array() Function
- Using the Colon Operator (:)
- Using the ones() Function
- Using the zeros() Function
- Using the rand() Function
- Initializing Multidimensional Arrays
- Copying and Concatenating Arrays
Using the array() Function
The array() function is the most common way to initialize an array. The array() syntax is:
where element1, element2, …, elementN are the elements of the array.
To create arrays containing 5 elements run:
Using the Colon Operator (:)
The colon operator (:) helps make an array of consecutive numbers. The syntax for the colon operator is:
Where the start is the starting index of the array, the end is the ending index of the array, and the step is the step size.
The below code will create a new array of numbers from 1 to 10:
Using the ones() Function
The ones() function is used to generate an array filled with the value one. It creates a new array where each element is set to the value of one.
The syntax of ones() in MATLAB is:
where rows are the number of rows in the array and columns are the number of columns in the array.
To create a new array in MATLAB using ones() function run the following code:
Using the zeros() Function
The zeros() function in MATLAB defines a new array containing all zeros. The zeros() function syntax is:
The above syntax defines the row and column for a new array in MATLAB.
The given code defines a new array containing 10 zeros:
Using the rand() Function
The rand() function in MATLAB defines an array containing all random numbers. The syntax for rand() is:
To create a 10 random numbers array use:
Initializing Multidimensional Arrays
To initialize multidimensional arrays in MATLAB, we can use the zeros or ones functions and specify the size of each dimension.
The given code defines a 3x4x2 array of zeros:
columns = 4;
depth = 2;
% Initialize the multidimensional array
array = zeros(rows, columns, depth);
% Display the array
disp(array);
This will create a 3x4x2 array where all elements are set to 0.
The above array is a 3-dimensional array with dimensions of 3 rows, 4 columns, and 2 depths. We initialize it with zeros using the zeros() function. After that, we displayed the array using disp().
Copying and Concatenating Arrays
We can also initialize an array by copying or concatenating existing arrays. To copy an array, we can use the assignment operator (=).
For example:
G = F
This will create a new array G that is a copy of F.
To concatenate arrays, we can use square brackets ([ ]).
For example:
I = [7:9;10:12]
J = [H; I]
This will concatenate arrays H and I vertically to create a new array J.
Conclusion
We can initialize arrays using different methods in MATLAB. Arrays store data inside them once initialized. In MATLAB we have an array() function which defines a new array. However, colon operators can also initialize an array by defining the starting and ending limits. Once you have initialized an array, you can use it to store data and perform calculations. Read the above article to cover all methods of initializing an array in MATLAB.