Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between vector, matrix and array data types?

Tags:

arrays

types

r

R comes with three types to store lists of homogenous objects: vector, matrix and array.

As far as I can tell:

  • vector is special cases for 1 dimension arrays
  • matrix is a special case for 2 dimensions arrays
  • array can also have any dimension level (including 1 and 2).

What is the difference between using 1D arrays over vectors and 2D arrays over matrices? Do we need to cast between those, or will it happen automagically?

like image 940
Eloims Avatar asked Nov 29 '15 12:11

Eloims


People also ask

What is the difference between a vector a matrix and an array?

4. A vector is an array of numbers with a single index while a matrix is an array of numbers with two indices. 5. While a vector is used to represent magnitude and direction, a matrix is used to represent linear transformations and keep track of coefficients in linear equations.

What is difference between array and matrix?

A matrix is a two-dimensional (r × c) object (think a bunch of stacked or side-by-side vectors). An array is a three-dimensional (r × c × h) object (think a bunch of stacked r × c matrices). All elements in an array must be of the same data type (character > numeric > logical).

Whats the difference between vector and matrix?

A vector is a list of numbers (can be in a row or column), A matrix is an array of numbers (one or more rows, one or more columns).

What is the difference between an array a matrix and a vector in Matlab?

Answer: We usually reserve the word "vector" to denote an array that consists of only one column , i.e. is m-by-1, or only one row, i.e is 1-by-n. An array in MATLAB is a generic word that can mean a vector, a matrix, or a higher dimensional object, such as a "matrix" with three or more indices.


1 Answers

There is no difference between a matrix and a 2D array:

> x <- matrix(1:10, 2) > y <- array(1:10, c(2, 5)) > identical(x, y) [1] TRUE ... 

matrix is just a more convenient constructor, and there are many functions and methods that only accept 2D arrays (a.k.a. matrices).

Internally, arrays are just vectors with a dimension attribute:

... > attributes(x) $dim [1] 2 5  > dim(x) <- NULL > x  [1]  1  2  3  4  5  6  7  8  9 10 > z <- 1:10 > dim(z) <- c(2, 5) > is.matrix(z) [1] TRUE 

To cite the language definition:

Matrices and arrays are simply vectors with the attribute dim and optionally dimnames attached to the vector.

[...]

The dim attribute is used to implement arrays. The content of the array is stored in a vector in column-major order and the dim attribute is a vector of integers specifying the respective extents of the array. R ensures that the length of the vector is the product of the lengths of the dimensions. The length of one or more dimensions may be zero.

A vector is not the same as a one-dimensional array since the latter has a dim attribute of length one, whereas the former has no dim attribute.

like image 113
Roland Avatar answered Oct 05 '22 06:10

Roland