# Array sizes

An array is a multidimensional data structure. We can determine and manipulate its features with the length, ncol, nrow and dim (dimension) functions.

The length function works on one-dimensional objects such as vectors and lists. In the example below we first create a vector V and then use `length()` to find out how long V is.

``````
Input:
V<-c(1:41)
length(V)

Output:
41
``````
``` ```

Matrices are two-dimensional objects so the length function will not work on them. With a two-dimensional object we need to specify the axis along which we want to find the size. This is done using the `nrow()` and `ncol()` functions to find out the number of rows and columns respectively. Let's first create a 3x3 matrix.

``````
Input:
M<-matrix(1:9,nrow=3)
M

Output:
1	4	7
2	5	8
3	6	9
``````
``` ```

Now we can use the `nrow()` and `ncol()` functions. The only arguments that they require is the name of the matrix.

``````
Input:
nrow(M)
ncol(M)

Output:
3
3
``````
``` ```

Knowing the number of rows and columns is useful for iterating over a matrix, for example using a for loop. The `dim()` function is an alternative to finding the rows and columns separately, it returns both the number of rows and the number of columns.

``````
Input:
dim(M)

Output:
3 3
``````
``` ```

Arrays can also be three-dimensional. As shown in the example below, two-dimensional arrays can be stacked on top of each other using the concatenation operator `c()` creating the third dimension. The `dim()` function can then be used to determine all three dimensions of the array.

``````
Input:
M2<-2*M
M3<-3*M
N<-array(c(M,M2,M3),c(3,3,3))
dim(N)

Output:
3 3 3
``````
``` ```