Rutils/maybe-not-useful/arr.r

#==========================================================================================#
#==========================================================================================#
#    This function is equivalent to "row" and "col", but works for arrays.  Two arguments  #
# are required, the array, and the dimension to extract.                                   #
#------------------------------------------------------------------------------------------#
arr <<- function(A,d){
   #----- Sanity check. -------------------------------------------------------------------#
   if (! (is.array(A) || is.data.frame(A) || is.matrix(A))){
      stop(" A must be an array, or at least a data frame or matrix!!!")
   }else{
      #----- Get dimensions and number of dimensions of A. --------------------------------#
      Adim   = dim(A)
      n.Adim = length(Adim)
      n.A    = length(A)
      #------------------------------------------------------------------------------------#


      #---- Make sure d is a valid dimension. ---------------------------------------------#
      if (! (d %wr% c(1,n.Adim))){
         cat(" d must be between 1 and ",n.Adim," (# of dimensions of A","\n",sep="")
         stop(" Invalid dimension request")
      }#end if
      #------------------------------------------------------------------------------------#
   }#end if
   #---------------------------------------------------------------------------------------#


   #---------------------------------------------------------------------------------------#
   #     Get the indices of A.                                                             #
   #---------------------------------------------------------------------------------------#
   A.idx = arrayInd(ind=sequence(n.A),.dim=Adim)[,d]
   return(A.idx)
   #---------------------------------------------------------------------------------------#
}#end function arr
#==========================================================================================#
#==========================================================================================#
manfredo89/ED2io documentation built on May 21, 2019, 11:24 a.m.