MATLAB MatLab: Understanding the Size Command

  • Thread starter Thread starter robax25
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary
The discussion clarifies how the size command operates in programming, particularly in relation to matrices. When using the size function on a matrix, the first argument specifies the dimension. For a 3x3 matrix, size(A) returns 3x3, indicating it has three rows and three columns. The commands size(A,1) and size(A,2) return 3, reflecting the number of rows and columns, respectively. However, size(A,3) returns 1 because the matrix is two-dimensional, and any dimension beyond the existing ones defaults to 1. This principle applies to higher-dimensional matrices as well; if a matrix is n-dimensional, size(A, dim) provides the size of that dimension, returning 1 for any dimension exceeding n. An example with a 3D matrix illustrates this further, showing how size commands behave with multiple dimensions.
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 RPinPA and jedishrfu

Similar threads

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