# How do I normalize the columns of a matrix?

How do I normalize the columns of a matrix?

such as $A = matrix(5,5,[\text{some numbers}])$

How do I normalize the columns of a matrix?

How do I normalize the columns of a matrix?

such as $A = matrix(5,5,[\text{some numbers}])$

add a comment

1

The simplest way to normalize the column of a matrix is probably to replace each column of a matrix by itself divided by its norm.

Create a matrix:

```
sage: a = matrix(RDF, 4, [randint(-10, 10) for _ in range(16)])
sage: a
[-2.0 6.0 -6.0 -2.0]
[ 6.0 1.0 -8.0 4.0]
[-7.0 4.0 -3.0 9.0]
[ 6.0 -9.0 9.0 -5.0]
```

Normalize each column:

```
sage: for j in range(a.ncols()):
....: v = a.column(j)
....: a[:,j] = v/v.norm()
....:
```

Show the result:

```
sage: a
[-0.17888543819998318 0.5183210553488161 -0.435285750066007 -0.1781741612749496]
[ 0.5366563145999496 0.08638684255813601 -0.5803810000880093 0.3563483225498992]
[ -0.6260990336999411 0.34554737023254406 -0.2176428750330035 0.8017837257372732]
[ 0.5366563145999496 -0.7774815830232241 0.6529286250990105 -0.44543540318737396]
```

Check the norms of the new columns:

```
sage: [c.norm() for c in a.columns()]
[1.0, 1.0, 1.0, 1.0]
```

Asked: **
2018-06-12 17:39:51 -0600
**

Seen: **1,587 times**

Last updated: **Jun 12 '18**

Row echelon form of a matrix containing symbolic expresssions

Generate a Matrix over a Finite Field with symbolic variables

Displaying matrices with . instead of 0

eigenvalues of a derivative vs derivative of eigenvalues

How to prevent memory leak when solving a linear system of equations using left_kernel ?

Is there an easy way to get the matrix of coefficients from a product of a matrix and a vector?

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.