Updated February 28, 2023

## Introduction to Size Function in MATLAB

MATLAB provides us with plenty of functionalities, useful in various computational problems. In this article, we will study a powerful MATLAB function called ‘MATLAB size’. Before we understand how to calculate size of an array in MATLAB, let us first understand what exactly SIZE function does. Size function in MATLAB will return a row vector, whose elements will be the size of the respective dimensions of the array passed in the input.

**Syntax of Size function in MATLAB:**

`A = size(Y)`

`[a,b] = size(Y)`

`A = size(Y,dim)`

`[dim1,dim2,dim3,...,dimN] = size(Y)`

### Description of Size Function in MATLAB

- A = size(Y), this function will return the size of each dimension of the array passed as input.
- [a, b] = size(Y), this function will return the size of input matrix in 2 separate variables ‘a’ and ‘b’
- A = size(Y,dim), this function will return the size of Y’s dimension, specified by the input scalar dim.
- [dim1,dim2,dim3,…,dimN] = size(Y), this function will return the size of ‘n’ dimensions of input array X in separate variables.

In case the number of arguments ‘n’ in output are not equal to ndims(Y), then if:

n > ndims(Y) |
The function will return those present in the extra variable |

n < ndims(Y) |
‘dn’ will contain the product of sizes of remaining dimensions of Y |

Now let us understand the computation of size in MATLAB with various examples:

### Examples to Implement Size Function in MATLAB

Below are the examples Size Function in MATLAB:

#### Example #1

Let us first define our input array as: rand(2, 4, 5)

As we can see in our input, the size of the third dimension in rand(2,4, 5) is 5.Let us try to find the same with the help of ‘size’ function.

- To find out the size of third dimension, this is how our input will look like in MATLAB:

a = size(rand(2, 4, 5), 3)

**Code:**

`a = size(rand(2, 4, 5), 3)`

a

**Output:**

- As we can see in the output, we have got the size of 3rd dimension, i.e, 5.

For the same input array, we can also get the size as one vector. Let us understand how we can achieve it:

a = size(rand(2, 4, 5))

This is how our input and output will look like in MATLAB console:

**Code:**

`a = size(rand(2, 4, 5))`

a

**Output:**

- As we can see in the output, we have got the size of all dimensions in single vector.

To assign sizes of dimensions to separate variables, we can use below code:

[a,b, c] = size(rand(2, 4, 5))a = 2

b =4

c =5

This is how our input and output will look like in MATLAB console:

**Code:**

`[a,b, c] = size(rand(2, 4, 5));`

a,b,c

**Output:**

As we can observe in the output, we have got size for all dimensions in separate variables.

#### Example #2

Let us first define a different input array.

A = ones(4, 3, 2)

Here, we are creating 2 arrays of size 4 x 3, with all ‘unity’ elements.

To get size of the dimension, below is our code:

[a, b, c] = size(A)This is how our input and output will look like in MATLAB console:

**Code:**

`A = ones(4, 3, 2)`

[a, b, c] = size(A)

**Output:**

#### Example #3

Next, let us take the example when our output variables are less than ndims (Y). Here also, we will use the same array as in the above example

[a, b] = size(Y)As we can see, the input has 3 size dimensions, but our output variables are only 2. In such cases, the last variable will be the product of all remaining dimensions.So, in our case, the product of last two dimensions is collapsed to last variable.

This is how our input and output will look like in MATLAB console:

**Code:**

`Y = ones (4,3,2)`

[a,b] = size(Y)

**Output:**

As we can observe in the output, the last variable contains product of 3 and 2, which are size of last 2 dimensions.

### Conclusion

So,in this article we learnt, size function can be used to calculate size of any array in MATLAB. Also, we can pass the number of arguments as per our requirement. MATLAB provides us with the product of size in case we pass less arguments than required.

### Recommended Articles

This is a guide to Size Function in MATLAB. Here we discuss the Introduction to size function in MATLAB along with its examples as well as code implementation. You can also go through our suggested articles to learn more –