This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.


Array dimensions


d = size(X)
[m,n] = size(X)
m = size(X,dim)
[d1,d2,d3,...,dn] = size(X),


d = size(X) returns the sizes of each dimension of array X in a vector, d, with ndims(X) elements.

  • If X is a scalar, then size(X) returns the vector [1 1]. Scalars are regarded as a 1-by-1 arrays in MATLAB®.

  • If X is a table, size(X) returns a two-element row vector consisting of the number of rows and the number of variables in the table. Variables in the table can have multiple columns, but size only counts the variables and rows.

[m,n] = size(X) returns the size of matrix X in separate variables m and n.

m = size(X,dim) returns the size of the dimension of X specified by scalar dim.

[d1,d2,d3,...,dn] = size(X), for n > 1, returns the sizes of the dimensions of the array X in the variables d1,d2,d3,...,dn, provided the number of output arguments n equals ndims(X). If n does not equal ndims(X), the following exceptions hold:

n < ndims(X)

di equals the size of the ith dimension of X for 0<i<n, but dn equals the product of the sizes of the remaining dimensions of X, that is, dimensions n through ndims(X).

n > ndims(X)

size returns ones in the "extra" variables, that is, those corresponding to ndims(X)+1 through n.

    Note   For a Java array, size returns the length of the Java array as the number of rows. The number of columns is always 1. For a Java array of arrays, the result describes only the top level array.


collapse all

Size of Dimensions in 3-D Array

Find the size of rand(2,3,4).

d = size(rand(2,3,4))
d =

     2     3     4

The size is output as a single vector.

Find the size of the second dimension of rand(2,3,4).

m = size(rand(2,3,4),2)
m =


The size is output as a scalar.

Assign the size of each dimension to a separate variable.

[m,n,p] = size(rand(2,3,4))
m =


n =


p =


Size of Table

Create a table with four variables listing patient information for five people.

LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];

X = table(Age,Height,Weight,BloodPressure,'RowNames',LastName)
X = 

                Age    Height    Weight    BloodPressure
                ___    ______    ______    _____________

    Smith       38     71        176       124     93   
    Johnson     43     69        163       109     77   
    Williams    38     64        131       125     83   
    Jones       40     67        133       117     75   
    Brown       49     64        119       122     80   

Find the size of the table.

d = size(X)
d =

     5     4

size counts four variables, even though the variable BloodPressure contains two columns.

Specify Different Number of Outputs than ndims(X)

Assign the size of each dimension to a separate variable where the number of outputs matches the number of dimensions.

X = ones(3,4,5);
[d1,d2,d3] = size(X)
d1 =


d2 =


d3 =


There is one output for each dimension of X.

Specify fewer output variables than ndims(X).

[d1,d2] = size(X)
d1 =


d2 =


The "extra" dimensions are collapsed into a single product.

Specify more output variables than ndims(X).

[d1,d2,d3,d4,d5,d6] = size(X)
d1 =


d2 =


d3 =


d4 =


d5 =


d6 =


The "extra" variables all represent singleton dimensions.

See Also

| | | |

Introduced before R2006a

Was this topic helpful?