Row- or column-wise normalizing of a two-dimensional floating point array which is interpreted as a matrix.
Mat Norm a(),i
a():name of a two-dimensional floating point array
i:ivar; i=0 for row-wise and i=1 for column-wise normalizing
Mat Norm a(),0 and Mat Norm a(),1 are used for both matrices and vectors. Mat Norm a(),0 normalizes a matrix (or a vector) row-wise and Mat Norm a(),1 normalizes a matrix (or a vector) column-wise. This means that in case of row-wise (column-wise) normalizing the sum of squares of all elements in each row (column) is equal to 1.
OpenW 1
Global a%, n% = 8, k%, i%
Global Double a(1 To n%, 1 To n%)
Global Double b(1 To n%, 1 To n%)
Global Double v(1 To n%), v(), x
Data 1,2,3,4,5,6,7,8
Data 3.2,4,-5,2.4,5.1,6.2,7.2,8.1
Data -2,-5,-6,-1.2,-1.5,-6.7,4.5,8.1
Data 5,-2.3,4,5.6,12.2,18.2,14.1,16
Data 4.1,5.2,16.7,18.4,19.1,20.2,13.6,14.8
Data 15.2,-1.8,13.6,-4.9,5.4,19.8,16.4,-20.9
Data -3.6,6,-8.2,-9.1,4,-2.5,2,3.4
Data 4.7,8.3,9.4,10.5,11,19,15.4,18.9
//
Mat Read a()
//save the original matrix
Mat Cpy b() = a()
Print "Original Matrix"
Mat Print a(), 7, 2
KeyPress
//
// row-wise normalising
//
Mat Norm a(), 0
Print "Row-wise normalised: "
Mat Print a(), 7, 2
KeyPress
//
// testing of the row-wise normalising
//
Print "Test: "
For i% = 1 To n%
Mat XCpy v() = a(i%, 1) // copies a() row-wise into vector v()
Mat Mul x = v()*v() // calculates the scalar product of v() and v()
Print x`
Next i%
KeyPress
// column-wise normalising
Mat Cpy a() = b()//copy the original matrix
Mat Norm a(), 1
Print "Column-wise normalised: "
Mat Print a(), 7, 2
KeyPress
// testing of column-wise normalising
Print "Probe : "
For i% = 1 To n%
Mat Cpy v() = a(1, i%) // copies a() column-wise into vector v()
Mat Mul x = v()*v()// calculates the scalar product of v() and v()
Print x`
Next i%
KeyPress
CloseW 1
Sub KeyPress
Local a%
Print "Press any key"
KeyGet a%
Cls
EndSub
-
{Created by Sjouke Hamstra; Last updated: 15/10/2014 by James Gaite}