Multi-Dimensional Arrays in MATLAB
- Arrays with one more than two dimensions are called multi-dimensional arrays.
- Multi-dimensional arrays are created with more than two subscripts in MATLAB.
- For example:
- Let’s create a three-dimensional array using function ones (3, 8, 3).
- This function creates a 3-by-8-by-3 array with a total of 3*8*3 = 72 elements.
- The third subscript tells to create no. of sets of elements in rows and columns as per first & second subscripts.
- Let’s have one more example:
- Here we use some more functions, and one of them is the perms function.
- The perms function returns all number of possible ways or permutations to arrange the elements of a matrix or vector in a different set of orders of a row vector.
An illustration of the above 3-D array:
Cell Arrays in MATLAB
A cell is the functional data object in MATLAB. It can contain any data, an array of numbers, strings, structures, or cells. An array of cells is called a cell array.
For example, one cell of a cell array contains an array of real numbers, another an array of strings, and yet another a vector of complex numbers.
In programming terms, each element of a cell array is a pointer to another data structure, and those data structures can be of different types.
Cell arrays provide a great way to collect information about a problem because all of the data can be kept together and accessed by a single name.
Cell arrays use braces :{}” instead of parentheses “()” for selecting and displaying the contents of cells. This difference is because cell arrays contain data structures instead of data. Suppose that the cell array a is defined as shown in the figure.
Then the contents of element a (1, 1) is a data structure containing a 3 x 3 array of numeric data, and a reference to a (1, 1) displays the contents of the cell, which is the data structure.
By contrast, a reference to a {1, 1} displays the contents of the data item contained in the cell.
Note: The notation a(1,1) refers to the contents of the cell a(1,1) (which is a data structure), while the notation a{1,1} relates to the contents of the data structure within the cell.
Creating Cell Arrays
A cell array can be created using cell function.
- We also can create a cell array using curly braces:
- Use the curly brace to access the elements of the cell array:
- Use cell array to store arrays of different sizes:
Structure Arrays in MATLAB
A structure is a data type in which each individual element has a name.
The individual elements of a structure are known as fields, and each field in a structure may have a different type.
The individual fields are addressed by combining the name of the structure with the name of the field, separated by a period.
Data in a field is accessed using dot notation.
Creating Structures
There are several ways to create a structure in MATLAB.
Creating a structure array using dot notation:
- The dot notation is used to name the fields of a structure array.
Adding elements to structure array
- Because structures are also arrays in MATLAB, so we can insert additional elements as per index.
- One thing to remember here is that all the field’s names should be the same while adding more elements.
- Structure of the command:
Adding elements with a single statement using struct command:
- We can add elements into the structure array with a single statement.
- All field and value pairs are defined within the struct command arguments.
Accessing elements of the structure array
- We can access any of the elements of the structure using an index.
- We can assign the value of an element to another variable.
Subarrays in MATLAB
It is desirable to select and use subsets of MATLAB arrays as though they were divided arrays. To select a portion of an array, containing a list of all of the items to be select in the parentheses after the array name.
For example, suppose array arr1 is described as follows:
Then arr1 (3) is just 3, arr1 ([1 4]) is the array [1.1 -4.4], and arr1 (1:2:5) is the array [1.1 3.3 5.5].
For a two-dimensional array, a colon can be used in a subscript to choose all of the values of that subscript. For example, suppose
This statement would create an array of arr2, including the values.
The subarray arr2 (1, 🙂 would be [1 2 3], and the subarray arr2 (:, 1:2:3) would be
The end Function
MATLAB includes a particular function named end that is very useful for creating array subscripts. When used in an array subscript, the end returns the highest value taken on by that subscript.
For example, suppose that array arr3 is described as follows:
Then arr3 (5: end) would be the array [5 6 7 8], and array (end) would be the value 8.
The value returned by the end is always the highest value of a given subscript. If the end appears in different subscripts, it can return different values within the same expression.
For example, suppose that the 3 x 4 array arr4 is defined as follows:
Then the expression arr4 (2: end, 2: end) would return the array.
Note: The first end returned the value 3, while the second end returned the value 4!