Special Arrays in MATLAB
In this first part, Mytour will introduce you to some functions for creating special arrays. Among these functions, a single parameter creates a square array, while two parameters create a rectangular array.
The zeros() function creates an array filled with all zeros.
For example:
zeros(5)
MATLAB will execute the above command and return the following result:
The ones() function generates an array filled with all ones.
For example:
ones(4,3)
MATLAB will execute the above command and return the following result:
The eye() function creates an identity matrix.
For example:
eye(4)
MATLAB will execute the above command and return the following result:
The rand () function generates an array of random numbers distributed over (0,1):
For example:
rand(3, 5)
MATLAB will execute the above command and return the following result:
Magic Square in MATLAB
A magic square in MATLAB is a square grid where the sum of numbers in each row, column, and both diagonals are equal.
The magic() function creates a magic square array. The size of the square must be specified as a single parameter. The parameter must be a positive integer greater than or equal to 3.
For example:
magic(4)
MATLAB will execute the above command and return the following result:
Multi-dimensional Arrays in MATLAB
Arrays with more than 2 dimensions are called multi-dimensional arrays in MATLAB. Multi-dimensional arrays in MATLAB are an extension of regular 2-dimensional matrices.
To create a multidimensional array, first create a 2-dimensional array and then expand it.
The following example creates a 2-dimensional array a.
a = [7 9 5; 6 1 9; 4 3 2]
MATLAB will execute the command above and return the following result:
Array a is a 3-column 3-row array. You can add a third dimension to array a by providing the following values:
a(:, :, 2)= [ 1 2 3; 4 5 6; 7 8 9]
MATLAB will execute the command above and return the following result:
Additionally, you can create multidimensional arrays using the functions ones(),zeros(), or rand().
For example:
b = rand(4,3,2)
MATLAB will execute the command above and return the following result:
You can also use the cat() function to create multidimensional arrays. This function concatenates a list of arrays along an unspecified dimension.
Syntax of the cat() function:
B = cat(dim, A1, A2...)
Where:
- B is the newly created array.
- A1, A2, ... are the arrays to be concatenated.
- dim is the dimension along which to concatenate the arrays.
For example: Create a script file and enter the following code snippet:
a = [9 8 7; 6 5 4; 3 2 1];
b = [1 2 3; 4 5 6; 7 8 9];
c = cat(3, a, b, [ 2 3 1; 4 7 8; 3 9 0])
When running the file, it will display the following result on the screen:
Array Functions in MATLAB
MATLAB provides the following functions to sort, rotate, permute, reshape, or alter array contents.
For example:
Below are illustrative examples for declaring arrays in MATLAB with some referenced functions above.
- Functions Length, Dimension, and Number:
Create a script file and enter the following code snippet:
When running the file, it will display the following result:
- Shifting around array elements:
Create a script file and enter the following code snippet:
When running the file, it will display the following result:
Array Classification
Create a script file and enter the following code snippet:
When running the file, it will display the following result:
Cell Arrays
Cell arrays in MATLAB are arrays of indexed cells, where each cell can store an array of varying size and data type.
The cell function is used to create cell arrays. Syntax of the cell function:
C = cell(dim)
C = cell(dim1,...,dimN)
D = cell(obj)
Where:
- C is the cell array.
- dim is an integer scalar or vector of specific integers that determine the size of cell array C.
- dim1, ..., dimN are integer scalars determining the size of C.
obj can be:
+ An array or Java object.
+ Arrays .NET of System.String or System.Object.
For example:
Create a script file and enter the following code snippet:
c = cell(2, 5);
Enclose indices in the first parentheses to reference a set of cells.
Enclose indices in curly braces to reference data within individual cells.
When enclosing indices in the first parentheses, it references a set of cells.
Array cell indices within square brackets include sets of cells.
For example:
Execute the following command in MATLAB:
The result of c(1:2,1:2)
is as follows:
MATLAB will execute the command and return the following result:
MATLAB executes the command and returns the following result:
ans = Blue
ans = Green
ans = Yellow
Here, Mytour has just introduced you to how to declare arrays in MATLAB. Additionally, you also need to grasp another declaration method which is variable declaration in MATLAB to differentiate from array declaration. If you have any inquiries or questions, feel free to leave your comments below the article, Mytour will address your concerns as soon as possible.