Get the Size of Vector in Matlab

Get Size of a Vector Using the
size()
Function in MATLAB 
Get the Number of Elements Present in a Vector Using the
length()
Function in MATLAB 
Get the Number of Elements Present in a Vector or Matrix Using the
numel()
Function in MATLAB
In this tutorial, we will discuss how to get the size and the number of elements present in a vector using the length()
, size()
, and numel()
functions in MATLAB.
Get Size of a Vector Using the size()
Function in MATLAB
The size()
function returns the number of rows and columns present in a vector or matrix. For example, let’s get the size of a vector. See the code below.
clc
vector = [1 2 3 4];
s = size(vector)
Output:
s =
1 4
As you can see, the size of the given vector is 1x4
. This method is only suitable for finding the size of a vector and not suitable for finding the number of elements as it returns the number of rows and columns.
Get the Number of Elements Present in a Vector Using the length()
Function in MATLAB
The length()
function returns the number of elements present in a vector. For example, let’s get the length of a vector. See the code below.
clc
vector = [1 2 3 4];
len = length(vector)
Output:
len =
4
As you can see, the length of the given vector is 4. This method is only suitable for finding the number of elements present in a vector and not suitable for finding the number of elements in a matrix as it returns only the longest dimension.
Get the Number of Elements Present in a Vector or Matrix Using the numel()
Function in MATLAB
The numel()
function returns the number of elements present in a vector or matrix. For example, let’s get the number of elements present in a matrix. See the code below.
clc
vector = [1 2 3 4; 1 2 3 4];
len = numel(vector)
Output:
len =
8
As you can see, the length or number of elements present in the given vector is 8. This method is suitable for finding the number of elements present in a vector or matrix.