On Aug 7, 2012, at 9:02 PM, Ingo Reinhold wrote:
Dear all,
I am facing a challenge when applying the outer product with a
matrix by rows.
What I have is a rather big matrix, which I would like to convert
into a different matrix by doing something like
outer(matrix_row, vector, function(x,y) x[1]+5*x[4]/y)
In order to get there, I tried to get the matrix into a list using
the mefa package, as subsetting the original data appears too
complicated. However, if I apply the following code I always get a
dimensional error, as the unlist() function is applied to the whole
list.
library(mefa)
a<-matrix(c(1:6),ncol=3)
b<-c(1,2,3,4)
a<-mat2list(a,MARGIN=1)
outer(a,b,function(x,y){unlist(a)[1]+unlist(a)[3]/b})
(You really should not be referring to a and b inside that function
body. It's arguments are now named x and y.)
Can you tell us what the answer _should_ be for the first entry? And a
step by step illustration of how you got there?
If this is not what it should be, then you need to read up on the
expected result of the division of a scalar by a vector in R.
apply(a, 1, function(x) { x[1] + x[3]/ b } )
[,1] [,2]
[1,] 6.000000 8.0
[2,] 3.500000 5.0
[3,] 2.666667 4.0
[4,] 2.250000 3.5
Any idea how to do this
Do "this" is still somewhat nebulous.
more elegantly than a nested for-loop?
Many thanks,
Ingo
David Winsemius, MD
Alameda, CA, USA
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.