MatLab: Understanding the Size Command

  • Context: MATLAB 
  • Thread starter Thread starter robax25
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary
SUMMARY

The discussion clarifies the functionality of the size command in MatLab, specifically regarding its behavior with multi-dimensional arrays. For a 3x3 matrix, size(A) returns 3x3, while size(A,1) and size(A,2) both return 3, and size(A,3) returns 1 due to the matrix being two-dimensional. It is established that for any n-dimensional array, size(A, dim) returns the size of the specified dimension, and dimensions exceeding the array's actual dimensions return 1.

PREREQUISITES
  • Understanding of MatLab syntax and commands
  • Basic knowledge of matrices and their dimensions
  • Familiarity with multi-dimensional arrays in programming
  • Experience with array manipulation in MatLab
NEXT STEPS
  • Explore MatLab's ones function for creating multi-dimensional arrays
  • Learn about MatLab's ndims function to determine the number of dimensions in an array
  • Investigate MatLab's array indexing techniques for accessing specific elements
  • Review MatLab documentation on array operations and dimensionality
USEFUL FOR

MatLab users, data analysts, and engineers who work with multi-dimensional arrays and need to understand array dimensions and their manipulation.

robax25
Messages
238
Reaction score
3
I have a little bit confusion that how size command work.can body explain please?. For Example
A=[1 2 3;4 5 6;7 8 9];
size(A)
=3 *3
but size(A,1)=3 and size(A,2)=3 and size(A,3)=1 why is size(A,3)=1?
 
Physics news on Phys.org
When you are doing ##size(A, 1)## or ##size(A, 2)##, 1 and 2 are dimensions here, not the row or column. Since the array is 3×3 (a two-dimensional matrix), the 1st dimension is 3, and the second is 3.

If you create a 3×3×3 matrix (a three-dimensional matrix), the 3rd dimension would be 3, but any higher order dimensions would be 1.

As a basic rule, if an array ##A## is n-dimensional, then ##size(A, dim)## returns the size of that dimension. If ##dim## exceeds ##n##, then 1 is returned.

Since you've created a two-dimensional array, all command that use dimensions > 2 return 1.

Try this and let me know whether you understand the output (don't copy and paste, as that often generates wrong outputs):
Matlab:
A = ones(2, 3, 4);
[sz1 sz2 sz3] = size(A)
[sz1 sz2 sz3 sz4 sz5] = size(A)

Read more here.

Edit: Grammar and formatting.
 
Last edited:
  • Like
Likes   Reactions: RPinPA and jedishrfu

Similar threads

  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K