Furkan Eris
Furkan Eris

Reputation: 3

R Euclidean norm of a Matrix

I have a matrix of data for a natural language processing course and I need to Euclidean normalize the matrix, I am very new to R so I cannot figure out what I am doing wrong with my code, heres the code:

A=matrix(
c(1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,1,0,0,1,1,0,0,0,0,1,0,0,1,2,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,1,0,0,0,1), 
nrow=11,
ncol=10)
norm_vec <- function(x) sqrt(sum(x^2))
B=matrix(
nrow=11,
ncol=10)
for(n in 1:10)
length <- norm_vec(A[,n])
for(j in 1:11)
B[j,n]<- A[j,n]/length

For this I get an output of only the last column being correct the other columns being NA and I do not understand why this is so. What am I doing wrong?

Upvotes: 0

Views: 1345

Answers (2)

ExperimenteR
ExperimenteR

Reputation: 4473

@Sathish's solution can go fully vectorized

 A / matrix(sqrt(colSums(A*A)), nrow=11, ncol=10, byrow=TRUE)

 #          [,1] [,2]      [,3]      [,4]      [,5]      [,6]      [,7]      [,8]      [,9]     [,10]
 #[1,] 0.5773503  0.0 0.0000000 0.4082483 0.5773503 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
 #[2,] 0.5773503  0.5 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
 #[3,] 0.5773503  0.5 0.5773503 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
 #[4,] 0.0000000  0.5 0.0000000 0.4082483 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
 #[5,] 0.0000000  0.5 0.0000000 0.8164966 0.5773503 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
 #[6,] 0.0000000  0.0 0.5773503 0.0000000 0.5773503 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
 #[7,] 0.0000000  0.0 0.5773503 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.7071068
 #[8,] 0.0000000  0.0 0.0000000 0.0000000 0.0000000 0.7071068 0.0000000 0.0000000 0.0000000 0.0000000
 #[9,] 0.0000000  0.0 0.0000000 0.0000000 0.0000000 0.7071068 0.7071068 0.5773503 0.7071068 0.0000000
 #[10,] 0.0000000  0.0 0.0000000 0.0000000 0.0000000 0.0000000 0.7071068 0.5773503 0.0000000 0.0000000
#[11,] 0.0000000  0.0 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.5773503 0.7071068 0.7071068

Upvotes: 2

Sathish
Sathish

Reputation: 12723

A=matrix(c(1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,1,0,0,1,1,0,0,0,0,1,0,0,1,2,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,1,0,0,0,1), nrow=11,  ncol=10)

norm_vec <- function(x){ 
                 sqrt(sum(x^2))
            }

B=matrix(nrow=11, ncol=10)

Euc.lenA <- c()

for(n in 1:10){ 
   Euc.lenA <- c(Euc.lenA, norm_vec(A[,n]))
}

for(j in 1:10){
   B[,j]<- A[,j]/Euc.lenA[j]
}

Euc.lenA
[1] 1.732051 2.000000 1.732051 2.449490 1.732051 1.414214 1.414214 1.732051 1.414214 1.414214

A and B

Upvotes: 0

Related Questions