
- MATLAB - Home
- MATLAB - Overview
- MATLAB - Features
- MATLAB - Environment Setup
- MATLAB - Editors
- MATLAB - Online
- MATLAB - Workspace
- MATLAB - Syntax
- MATLAB - Variables
- MATLAB - Commands
- MATLAB - Data Types
- MATLAB - Operators
- MATLAB - Dates and Time
- MATLAB - Numbers
- MATLAB - Random Numbers
- MATLAB - Strings and Characters
- MATLAB - Text Formatting
- MATLAB - Timetables
- MATLAB - M-Files
- MATLAB - Colon Notation
- MATLAB - Data Import
- MATLAB - Data Output
- MATLAB - Normalize Data
- MATLAB - Predefined Variables
- MATLAB - Decision Making
- MATLAB - Decisions
- MATLAB - If End Statement
- MATLAB - If Else Statement
- MATLAB - If…Elseif Else Statement
- MATLAB - Nest If Statememt
- MATLAB - Switch Statement
- MATLAB - Nested Switch
- MATLAB - Loops
- MATLAB - Loops
- MATLAB - For Loop
- MATLAB - While Loop
- MATLAB - Nested Loops
- MATLAB - Break Statement
- MATLAB - Continue Statement
- MATLAB - End Statement
- MATLAB - Arrays
- MATLAB - Arrays
- MATLAB - Vectors
- MATLAB - Transpose Operator
- MATLAB - Array Indexing
- MATLAB - Multi-Dimensional Array
- MATLAB - Compatible Arrays
- MATLAB - Categorical Arrays
- MATLAB - Cell Arrays
- MATLAB - Matrix
- MATLAB - Sparse Matrix
- MATLAB - Tables
- MATLAB - Structures
- MATLAB - Array Multiplication
- MATLAB - Array Division
- MATLAB - Array Functions
- MATLAB - Functions
- MATLAB - Functions
- MATLAB - Function Arguments
- MATLAB - Anonymous Functions
- MATLAB - Nested Functions
- MATLAB - Return Statement
- MATLAB - Void Function
- MATLAB - Local Functions
- MATLAB - Global Variables
- MATLAB - Function Handles
- MATLAB - Filter Function
- MATLAB - Factorial
- MATLAB - Private Functions
- MATLAB - Sub-functions
- MATLAB - Recursive Functions
- MATLAB - Function Precedence Order
- MATLAB - Map Function
- MATLAB - Mean Function
- MATLAB - End Function
- MATLAB - Error Handling
- MATLAB - Error Handling
- MATLAB - Try...Catch statement
- MATLAB - Debugging
- MATLAB - Plotting
- MATLAB - Plotting
- MATLAB - Plot Arrays
- MATLAB - Plot Vectors
- MATLAB - Bar Graph
- MATLAB - Histograms
- MATLAB - Graphics
- MATLAB - 2D Line Plot
- MATLAB - 3D Plots
- MATLAB - Formatting a Plot
- MATLAB - Logarithmic Axes Plots
- MATLAB - Plotting Error Bars
- MATLAB - Plot a 3D Contour
- MATLAB - Polar Plots
- MATLAB - Scatter Plots
- MATLAB - Plot Expression or Function
- MATLAB - Draw Rectangle
- MATLAB - Plot Spectrogram
- MATLAB - Plot Mesh Surface
- MATLAB - Plot Sine Wave
- MATLAB - Interpolation
- MATLAB - Interpolation
- MATLAB - Linear Interpolation
- MATLAB - 2D Array Interpolation
- MATLAB - 3D Array Interpolation
- MATLAB - Polynomials
- MATLAB - Polynomials
- MATLAB - Polynomial Addition
- MATLAB - Polynomial Multiplication
- MATLAB - Polynomial Division
- MATLAB - Derivatives of Polynomials
- MATLAB - Transformation
- MATLAB - Transforms
- MATLAB - Laplace Transform
- MATLAB - Laplacian Filter
- MATLAB - Laplacian of Gaussian Filter
- MATLAB - Inverse Fourier transform
- MATLAB - Fourier Transform
- MATLAB - Fast Fourier Transform
- MATLAB - 2-D Inverse Cosine Transform
- MATLAB - Add Legend to Axes
- MATLAB - Object Oriented
- MATLAB - Object Oriented Programming
- MATLAB - Classes and Object
- MATLAB - Functions Overloading
- MATLAB - Operator Overloading
- MATLAB - User-Defined Classes
- MATLAB - Copy Objects
- MATLAB - Algebra
- MATLAB - Linear Algebra
- MATLAB - Gauss Elimination
- MATLAB - Gauss-Jordan Elimination
- MATLAB - Reduced Row Echelon Form
- MATLAB - Eigenvalues and Eigenvectors
- MATLAB - Integration
- MATLAB - Integration
- MATLAB - Double Integral
- MATLAB - Trapezoidal Rule
- MATLAB - Simpson's Rule
- MATLAB - Miscellenous
- MATLAB - Calculus
- MATLAB - Differential
- MATLAB - Inverse of Matrix
- MATLAB - GNU Octave
- MATLAB - Simulink
MATLAB - Multi-dimensional Array
A multi-dimensional array in matlab is an array with more than 2 dimensions. We have so far come across two dimensions represented with rows and columns. So to access any element you need to specify the row index and column index. In the case of a multidimensional array it is an extension to the 2 dimension array.
Multi-dimensional arrays are an important concept in MATLAB and they are used to store data in multiple dimensions, such as matrices and higher-dimensional arrays. They are essential for tasks involving numerical computations, data analysis, and image processing.
Advantages of Multi-dimensional Array
Here is a list of advantages and disadvantages that I can think of for multi-dimensional arrays.
- MATLAB is designed for numerical computation and data analysis, making multi-dimensional arrays easy to create, manipulate, and perform computations on.
- MATLAB's underlying architecture is optimized for array operations, which allows for efficient handling of large datasets and complex computations.
- Newcomers can easily get familiar with the concept of multi-dimensional arrays featured in matlab and make use of it in their work.
- MATLAB's powerful visualization capabilities enable you to plot, visualize, and analyze multi-dimensional data using various types of plots, graphs, and images.
Disadvantages of Multi-dimensional Array
- Multi-dimensional arrays can make use of significant memory, especially for large datasets. Handling massive arrays might lead to memory limitations on your system.
- Cost effective and performance can be a little slow.
Create Multi-dimensional Arrays
To create a multi dimensional array, first let us create a 2 dimensional array and later extend it into a multidimensional array.
Let us create a 3x3 2D array as shown below −
A = [1 2 3; 4 5 6; 7 8 9]
The output is command window is as follows −
>> A = [1 2 3; 4 5 6; 7 8 9] A = 1 2 3 4 5 6 7 8 9 >>
Now to add the third dimension you can do it as follows −
A(:,:,2) = [10 11 12; 13 14 15; 16 17 18]
So A(:,:, 1) has [1 2 3; 4 5 6; 7 8 9] and A(:,:,2) has [10 11 12; 13 14 15; 16 17 18]
Let us see the execution in matlab command window −
>> A = [1 2 3; 4 5 6; 7 8 9] A = 1 2 3 4 5 6 7 8 9 >> A(:,:,2) = [10 11 12; 13 14 15; 16 17 18] A(:,:,1) = 1 2 3 4 5 6 7 8 9 A(:,:,2) = 10 11 12 13 14 15 16 17 18 >> A(:,:, 1) ans = 1 2 3 4 5 6 7 8 9 >> A(:,:,2) ans = 10 11 12 13 14 15 16 17 18 >>
Let us add the 3rd and 4th page to the multidimensional array A.
A(:,:,3) = 0 A(:,:,4) = 1
When you execute the same in matlab command window −
A(:,:,3) = 0 0 0 0 0 0 0 0 0 A(:,:,4) = 1 1 1 1 1 1 1 1 1
So when you check the matrix A in matlab command window you will get the following −
>> A A(:,:,1) = 1 2 3 4 5 6 7 8 9 A(:,:,2) = 10 11 12 13 14 15 16 17 18 A(:,:,3) = 0 0 0 0 0 0 0 0 0 A(:,:,4) = 1 1 1 1 1 1 1 1 1 >>
Accessing Elements of Multi-dimensional Array
To access elements from a multidimensional array make use of integer subscripts for the row, columns and the page you want to access.
For example, in a 2D array (matrix), you would use two subscripts: one for the row index and one for the column index. In a 3D array, you would use three subscripts: one for the row index, one for the column index, and one for the "page" or depth index.
Considering the values in each page as shown below we are going to access elements from the multidimensional array A.
A(:,:,1) = [1 2 3; 4 5 6; 7 8 9] A(:,:,2) = [10 11 12; 13 14 15; 16 17 18]
On execution in matlab command window the array A looks as follows −
>> A(:,:,1) = [1 2 3; 4 5 6; 7 8 9] A(:,:,2) = [10 11 12; 13 14 15; 16 17 18] A(:,:,1) = 1 2 3 4 5 6 7 8 9 A(:,:,2) = 10 11 12 13 14 15 16 17 18
From above from page 2 I want to access the value : 12.
For that we can have the following details to access it
A(1, 3, 2)
Here 1 is the row in which the value 12 is present, 3 is the column in which it is present and 2 is the page or depth.
When you execute the same in matlab command window −
>> A(1, 3, 2) ans = 12 >>
So earlier we have accessed only one value , to access range of values it can be done as follows −
Use the index vector [1 2] in the second dimension to access only the first and 2nd columns of each page of A.
C = A(:,[1 2],:)
When you execute the same the output in matlab command window is −
C(:,:,1) = 1 2 4 5 7 8 C(:,:,2) = 10 11 13 14 16 17
Now if you want to fetch the 2nd and 3rd row of each page you can do as follows −
D = A(2:3,:,:)
When you execute the same the output in matlab command window is −
>> D = A(2:3,:,:) D(:,:,1) = 4 5 6 7 8 9 D(:,:,2) = 13 14 15 16 17 18
Manipulating Multi-dimensional Arrays
In multidimensional arrays the elements can be rearranged in a variety of manners, much like what can be done with vectors and matrices. The functions reshape, permute, and squeeze prove to be valuable tools for adjusting the arrangement of these elements.
Let us see the examples of reshape(), permute() and squeeze() on the 2 page multidimensional array A.
A = [1 2 3 4 5; 9 0 6 3 7; 8 1 5 0 2] A(:,:,2) = [9 7 8 5 2; 3 5 8 5 1; 6 9 4 3 3]
When you execute above in matlab command window the output is −
>> A = [1 2 3 4 5; 9 0 6 3 7; 8 1 5 0 2] A(:,:,2) = [9 7 8 5 2; 3 5 8 5 1; 6 9 4 3 3] A = 1 2 3 4 5 9 0 6 3 7 8 1 5 0 2 A(:,:,1) = 1 2 3 4 5 9 0 6 3 7 8 1 5 0 2 A(:,:,2) = 9 7 8 5 2 3 5 8 5 1 6 9 4 3 3 >>
reshape()
Let us try the reshape() method on A.
reshaped_A = reshape(A, [], size(A, 3))
Here, you've used the reshape function to transform the 3-D array A into a 2-D matrix reshaped_A. The function reshape(A, [], size(A, 3)) rearranges the elements of A into a matrix where the number of rows is determined automatically based on the number of pages in A.
When you execute the same in matlab command window you will get −
>> reshaped_A = reshape(A, [], size(A, 3)) reshaped_A = 1 9 9 3 8 6 2 7 0 5 1 9 3 8 6 8 5 4 4 5 3 5 0 3 5 2 7 1 2 3 >>
permute()
permuted_A = permute(A, [3, 1, 2])
The permute function reorders the dimensions of the original 3-D array A. By specifying [3, 1, 2] as the permutation order, you've moved the "page" dimension to the front, changing the arrangement of elements in the array.
On execution in matlab command window the output is −
>> permuted_A = permute(A, [3, 1, 2]) permuted_A(:,:,1) = 1 9 8 9 3 6 permuted_A(:,:,2) = 2 0 1 7 5 9 permuted_A(:,:,3) = 3 6 5 8 8 4 permuted_A(:,:,4) = 4 3 0 5 5 3 permuted_A(:,:,5) = 5 7 2 2 1 3 >>
squeeze()
squeezed_A = squeeze(A)
The squeeze function can be used to remove singleton dimensions from the given 3-D array A, resulting in a 3-D array squeezed_A .
On execution in matlab command window the output is −
>> squeezed_A = squeeze(A) squeezed_A(:,:,1) = 1 2 3 4 5 9 0 6 3 7 8 1 5 0 2 squeezed_A(:,:,2) = 9 7 8 5 2 3 5 8 5 1 6 9 4 3 3 >>