MatLab: Understanding the Size Command

  • MATLAB
  • Thread starter robax25
  • Start date
  • Tags
    Matlab
In summary, the conversation discusses the functionality of the size command in MATLAB. It is explained that when using size(A, 1) or size(A, 2), the numbers 1 and 2 refer to dimensions, not rows or columns. Therefore, in a 3x3 matrix, the first and second dimensions are both 3. In a higher order matrix, any dimensions above the third would return a size of 1. It is also noted that if the specified dimension is greater than the number of dimensions in the array, 1 will be returned. Further clarification and a helpful example are provided.
  • #1
robax25
238
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
  • #2
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

1. What is the purpose of the size command in MatLab?

The size command in MatLab is used to determine the dimensions of a matrix or array. It provides the number of rows and columns in the matrix, as well as the total number of elements.

2. How do I use the size command in MatLab?

To use the size command, simply type "size" followed by the name of the matrix or array you want to check the dimensions of. For example, if your matrix is named "A", you would type "size(A)" and press enter.

3. Can the size command be used on non-numeric data in MatLab?

No, the size command can only be used on numeric data in MatLab. If you try to use it on non-numeric data, you will receive an error message.

4. Can the size command be used to change the dimensions of a matrix in MatLab?

No, the size command only provides information about the current dimensions of a matrix. To change the dimensions, you would need to use other MatLab commands such as reshape or resize.

5. Are there any limitations to using the size command in MatLab?

The size command can only be used on matrices or arrays with a maximum of 2 dimensions. If you need to determine the dimensions of a multidimensional array, you will need to use other MatLab commands such as ndims.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
352
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top