EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Size Function in MATLAB
 

Size Function in MATLAB

Priya Pedamkar
Article byPriya Pedamkar

Updated February 28, 2023

Size Function in MATLAB

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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

  1. A = size(Y), this function will return the size of each dimension of the array passed as input.
  2. [a, b] = size(Y), this function will return the size of input matrix in 2 separate variables ‘a’ and ‘b’
  3. A = size(Y,dim), this function will return the size of Y’s dimension, specified by the input scalar dim.
  4. [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:

Size Function in MATLAB 1

  • 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:

Size Function in MATLAB 1

  • 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:

Size Function in MATLAB 1

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:

Unity Elements Example 2

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:

Dimensions Example 3

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 –

  1. Introduction to MATLAB Functions
  2. Top 10 Advantages of Matlab
  3. Overview of Mean Function in Matlab
  4. How to Use Matlab? | Operators Used

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW