0

Update for more attempted clarity: I did investigate both my books and many sites and I'm sure I simply didn't search the right key words.

I created a function that multiplies matrices times vectors (full matrix multiplication) which you'll see in the code. So far that's fine and working. I input my function and its arguments:

m<- matrix(1:9, nrow= 3, ncol=3)
f <- function(x,y,z) {(as.vector(y)%*%x%*%as.vector(y))*z}
f(m,rep(1,3),2)

This outputs: [,1] [1,] 90

x and y remain constant but z will vary.

So I want use a vector to get multiple outputs.

z<- as.vector(1,2,3)

I've tried the apply function like so:

apply (c(rep(m,3),rep(1,3),rep(2,3)),1,f)

But that produces an error.

Error: unexpected ',' in "apply ((rep(m,3),"

So my question is how can you do matrix multiplication and then multiply that output to each element in a vector for plotting?

Also I realize that one answer is to simply create a function that performs the matrix multiplication first and then another that multiplies that result times a scalar but can we assume that isn't available as a solution.

Thanks in advance for any help

rsgmon
  • 1,892
  • 4
  • 23
  • 35
  • What you want is unclear: you want a "vector of outputs", but what part of the inputs should vary? Your error is just a missing `c` in `c(rep(...`, but you probably want to use `sapply` if only one part of the input varies, or `mapply` if several vary. – Vincent Zoonekynd Feb 01 '12 at 06:05

1 Answers1

0

Your function takes 3 arguments but when you put it in the apply you just put it in as f. This (using just f) would work if you only have one argument in the function or they are preset to a specific value but your function does not have y and z preset.

also this snipet here isn't anything. You need to store it as a vector, list etc

(rep(m,3),rep(1,3),rep(2,3))

so you'd need

c(rep(m,3),rep(1,3),rep(2,3))

or

list(rep(m,3),rep(1,3),rep(2,3))

I really don't know what you're trying to accomplish so it may be helpful to give us an example of what you expect the out put to be.

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519