Processing math: 100%

First time here? Check out the FAQ!

Sorry, this content is no longer available

Ask Your Question
2

How do I normalize the columns of a matrix?

asked 6 years ago

ionsme gravatar image

How do I normalize the columns of a matrix?

such as A=matrix(5,5,[some numbers])

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 6 years ago

slelievre gravatar image

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]
Preview: (hide)
link

Comments

carefully,I would add an if ?:

for j in range(a.ncols()):
    v = a.column(j)
    if v.norm() <> 0 :
        a[:,j] = v/v.norm()
ortollj gravatar imageortollj ( 5 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 6 years ago

Seen: 5,322 times

Last updated: Jun 13 '18