On this page:
list->matrix
matrix->list
vector->matrix
matrix->vector
->row-matrix
->col-matrix
list*->matrix
matrix->list*
vector*->matrix
matrix->vector*

7.4 Conversion

procedure

(list->matrix m n xs)  (Matrix A)

  m : Integer
  n : Integer
  xs : (Listof A)

procedure

(matrix->list M)  (Listof A)

  M : (Matrix A)
Convert a flat list to an m×n matrix and back; both m and n must be positive, and (* m n) = (length xs). The entries in xs are in row-major order.

Examples:
> (list->matrix 2 3 '(1 2 3 4 5 6))

- : (Array Positive-Byte)

(array #[#[1 2 3] #[4 5 6]])

> (matrix->list (matrix [[1 2] [3 4] [5 6]]))

- : (Listof Positive-Byte)

'(1 2 3 4 5 6)

procedure

(vector->matrix m n xs)  (Matrix A)

  m : Integer
  n : Integer
  xs : (Vectorof A)

procedure

(matrix->vector M)  (Vectorof A)

  M : (Matrix A)
Like list->matrix and matrix->list, but for vectors.

Examples:
> (vector->matrix 2 3 #(1 2 3 4 5 6))

- : (Mutable-Array Integer)

(mutable-array #[#[1 2 3] #[4 5 6]])

> (matrix->vector (matrix [[1 2] [3 4] [5 6]]))

- : (Vectorof Integer)

'#(1 2 3 4 5 6)

procedure

(->row-matrix xs)  (Matrix A)

  xs : (U (Listof A) (Vectorof A) (Array A))

procedure

(->col-matrix xs)  (Matrix A)

  xs : (U (Listof A) (Vectorof A) (Array A))
Convert a list, vector, or array into a row or column matrix. If xs is an array, it must be nonempty and not have more than one axis with length greater than 1.

Examples:
> (->row-matrix '(1 2 3))

- : (Array Positive-Byte)

(array #[#[1 2 3]])

> (->row-matrix #(1 2 3))

- : (Array Positive-Byte)

(array #[#[1 2 3]])

> (->row-matrix (col-matrix [1 2 3]))

- : (Array Positive-Byte)

(array #[#[1 2 3]])

> (->col-matrix (array #[#[#[1]] #[#[2]] #[#[3]]]))

- : (Array Positive-Byte)

(array #[#[1] #[2] #[3]])

> (->col-matrix (matrix [[1 0] [0 1]]))

array->col-matrix: contract violation

  expected: nonempty Array with exactly one axis of length

>= 1

  given: (array #[#[1 0] #[0 1]])

procedure

(list*->matrix xss)  (Matrix A)

  xss : (Listof (Listof A))

procedure

(matrix->list* M)  (Listof (Listof A))

  M : (Matrix A)
Convert a list of lists of entries into a matrix and back.

Examples:
> (list*->matrix '((1 2 3) (4 5 6)))

- : (Array Positive-Byte)

(array #[#[1 2 3] #[4 5 6]])

> (matrix->list* (matrix [[1 2 3] [4 5 6]]))

- : (Listof (Listof Positive-Byte))

'((1 2 3) (4 5 6))

These functions are like list*->array and array->list*, but use a fixed-depth (i.e. non-recursive) list type, and do not require a predicate to distinguish entries from rows.

procedure

(vector*->matrix xss)  (Matrix A)

  xss : (Vectorof (Vectorof A))

procedure

(matrix->vector* M)  (Vectorof (Vectorof A))

  M : (Matrix A)
Like list*->matrix and matrix*->list, but for vectors.

Examples:
> ((inst vector*->matrix Integer) #(#(1 2 3) #(4 5 6)))

- : (Mutable-Array Integer)

(mutable-array #[#[1 2 3] #[4 5 6]])

> (matrix->vector* (matrix [[1 2 3] [4 5 6]]))

- : (Vectorof (Vectorof Integer))

'#(#(1 2 3) #(4 5 6))

As in the first example, Typed Racket often needs help inferring the type A.