I try to utilize some operations on rows in a matrix using command
'apply'
but find a problem.
First I write a simple function to normalize a vector (ignore error
handling) as follows:
normalize = function( v ) {
return( ( v-min(v) ) / ( max(v) - min(v) ) )
}
The function works fine for a vector: > normalize( 1:5 )
[1] 0.00 0.25 0.50 0.75 1.00
Then I generate a matrix:> a = c(1:5) %*% t(1:5)
> a
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 2 4 6 8 10
[3,] 3 6 9 12 15
[4,] 4 8 12 16 20
[5,] 5 10 15 20 25
The function works fine upon the columns:> apply( a, 2, normalize )
[,1] [,2] [,3] [,4] [,5]
[1,] 0.00 0.00 0.00 0.00 0.00
[2,] 0.25 0.25 0.25 0.25 0.25
[3,] 0.50 0.50 0.50 0.50 0.50
[4,] 0.75 0.75 0.75 0.75 0.75
[5,] 1.00 1.00 1.00 1.00 1.00
BUT it does not work upon the rows:> apply( a, 1, normalize )
[,1] [,2] [,3] [,4] [,5]
[1,] 0.00 0.00 0.00 0.00 0.00
[2,] 0.25 0.25 0.25 0.25 0.25
[3,] 0.50 0.50 0.50 0.50 0.50
[4,] 0.75 0.75 0.75 0.75 0.75
[5,] 1.00 1.00 1.00 1.00 1.00
Can anyone help me to solve this problem? Thanks in advance!
--
View this message in context:
http://r.789695.n4.nabble.com/Problem-with-command-apply-tp2295845p2295845.html
Sent from the R help mailing list archive at Nabble.com.
litao.ext wrote:> > I try to utilize some operations on rows in a matrix using command 'apply' > but find a problem. > > First I write a simple function to normalize a vector (ignore error > handling) as follows: > normalize = function( v ) { > return( ( v-min(v) ) / ( max(v) - min(v) ) ) > } > > The function works fine for a vector: >> normalize( 1:5 ) > [1] 0.00 0.25 0.50 0.75 1.00 > > Then I generate a matrix: >> a = c(1:5) %*% t(1:5) >> a > [,1] [,2] [,3] [,4] [,5] > [1,] 1 2 3 4 5 > [2,] 2 4 6 8 10 > [3,] 3 6 9 12 15 > [4,] 4 8 12 16 20 > [5,] 5 10 15 20 25 >Your matrix is symmetric. /Berend -- View this message in context: http://r.789695.n4.nabble.com/Problem-with-command-apply-tp2295845p2295945.html Sent from the R help mailing list archive at Nabble.com.
yes, the matrix is symmetric. But when I apply normalize upon rows, I expect to get the following result:> apply( a, 1, normalize )[,1] [,2] [,3] [,4] [,5] [1,] 0.00 0.25 0.50 0.75 1.00 [2,] 0.00 0.25 0.50 0.75 1.00 [3,] 0.00 0.25 0.50 0.75 1.00 [4,] 0.00 0.25 0.50 0.75 1.00 [5,] 0.00 0.25 0.50 0.75 1.00 Actually we can get asymmetric matrix like:> a = c(1:5) %*% t(1:5) > a[,1] [,2] [,3] [,4] [,5] [1,] 5 4 3 2 1 [2,] 10 8 6 4 2 [3,] 15 12 9 6 3 [4,] 20 16 12 8 4 [5,] 25 20 15 10 5> apply(a, 2, normalize)[,1] [,2] [,3] [,4] [,5] [1,] 0.00 0.00 0.00 0.00 0.00 [2,] 0.25 0.25 0.25 0.25 0.25 [3,] 0.50 0.50 0.50 0.50 0.50 [4,] 0.75 0.75 0.75 0.75 0.75 [5,] 1.00 1.00 1.00 1.00 1.00> > apply(a, 1, normalize)[,1] [,2] [,3] [,4] [,5] [1,] 1.00 1.00 1.00 1.00 1.00 [2,] 0.75 0.75 0.75 0.75 0.75 [3,] 0.50 0.50 0.50 0.50 0.50 [4,] 0.25 0.25 0.25 0.25 0.25 [5,] 0.00 0.00 0.00 0.00 0.00 the latter result is very strange. -- View this message in context: http://r.789695.n4.nabble.com/Problem-with-command-apply-tp2295845p2296018.html Sent from the R help mailing list archive at Nabble.com.