matlab - How to know a dimension of matrix or vector in R? -
matlab - How to know a dimension of matrix or vector in R? -
i want find function in r same function size in matlab.
in matlab, if a = [ 1 2 3 4 5], size(a) = 1 5.
if a =[ 1 2 3;4 5 6], size(a) = 3 3.
in r, found function dim gives size of matrix, doesn't apply vectors.
please help me solve problem.
thanks lot.
as noted dim doesn't work on vectors. can utilize function take number of vectors matrices, data.frames or lists , find dimension or length:
dim <- function( ... ){ args <- list(...) lapply( args , function(x) { if( is.null( dim(x) ) ) return( length(x) ) dim(x) } ) } # length 10 vector <- 1:10 # 3x3 matrix b <- matrix(1:9,3,3) # length 2 list c <- list( 1:2 , 1:100 ) # 1 row, 2 column data.frame d <- data.frame( =1 , b = 2 ) dim(a,b,c,d) #[[1]] #[1] 10 #[[2]] #[1] 3 3 #[[3]] #[1] 2 #[[4]] #[1] 1 2 r matlab
Comments
Post a Comment