+ Reply to Thread
Results 1 to 3 of 3

writing function in VBA

Hybrid View

johnboy12 writing function in VBA 06-21-2008, 03:51 PM
Leith Ross Hello johnboy12, To make... 06-21-2008, 04:08 PM
johnboy12 Thanks Leith. John 06-21-2008, 05:23 PM
  1. #1
    Registered User
    Join Date
    06-14-2008
    Posts
    12

    writing function in VBA

    Hi everyone,

    I've got a function written in R that I want to convert into VBA. The function takes in 2 arguments a matrix of data and a tolerance level (scalar). I ideally would like the data input in an Excel spreadsheet - matrix and scalar and output the data to a location on another spreadsheet. This is my function in R:
    # read in matrix 'x' and scalar 'tol'
    # x is a square matrix
    PowerMethod <- function(x, tol) {   
       # calculate covariance matrix of x
    	my.mat <- var(x, na.method="available")
      # find the number of rows or columns of x
    	matSize <- dim(my.mat)[1]
      # create a new matrix of the output data
    	eigenVec <- matrix(NA, nrow=matSize, ncol=matSize)
    	eigenVal <- rep(NA, matSize)
     # for the length of the rows/columns of x do this
    	for(j in 1:matSize) {
     # initial guess for x
    		x <- rep(1, matSize)
     # calculate yk
    		yk <- my.mat%*%x
     # n1 and n2 are the normalised vectors of x and yk for the tolerance
    		n1 <- (1/sqrt(sum(x^2)))*x
    		n2 <- (1/sqrt(sum(yk^2)))*yk
     # if tolerance level isn't satisfied then do while loop
     		while(any(abs(n1-n2)>tol)) {
     # calculate beta which is the element of yk with the largest magnitude
    			beta <- yk[abs(yk)==max(abs(yk))]
     # update the value of x
    			x <- (1/beta)*yk
    			yk <- my.mat%*%x
     # update the values of n1 and n2
    			n1 <- (1/sqrt(sum(x^2)))*x
    			n2 <- (1/sqrt(sum(yk^2)))*yk
    		}
     # put the values of n1 and beta into the output matrix and vector
    		eigenVec[,j] <- n1
    		eigenVal[j] <- beta
     # update the value of the covariance matrix
    		my.mat <- my.mat - eigenVal[j]*eigenVec[,j]%*%t(eigenVec[,j])
    	}
     # output the matrix and vector of eigenvectors and eigenvalues 
    	list(values=eigenVal, vectors=eigenVec)
    }
    Would really appreciate any help on this.

    John
    Last edited by Leith Ross; 06-21-2008 at 04:07 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello johnboy12,

    To make your posts easier to read and preserve your formatting, please wrap your code. I did it for you this time. Here is how you can do it next time.

    How to wrap your Code
    On the Message window Toolbar you will see the # icon. This will automatically wrap the text you selected with the proper HTML tags to create a Code Window in your post. You can do this manually by placing the tag [code] at the start of the line, and the tag [/code] at the end.

    As a new member please take so time to familiarize yourself with the Do's and Don'ts here in the Forum, just click on the link below...

    Forum Rules

    Sincerely,
    Leith Ross

  3. #3
    Registered User
    Join Date
    06-14-2008
    Posts
    12
    Thanks Leith.

    John

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1