chumpy town
2004-Sep-25 21:30 UTC
[R] making custom function compute/return multiple items
Hello, I'm relatively new to R. I've read the intro guide, but I
can't quite figure out how to:
I have a function:
Jcost <- function (theta, in, out) {
a <- output - input %*% theta
1/2 * t(a) %*% a
}
where
"theta" is a 2x1 matrix
"in" is a 20x2 matrix
"out" is a 20x1 matrix
return value is a scaler
This works well when I only want to compute given 1 theta matrix. How
do I compute several (say N) 2x1 theta matrices and get back N scaler
values?
Thanks!
--
-david
David Chu
Peter Dalgaard
2004-Sep-25 21:53 UTC
[R] making custom function compute/return multiple items
chumpy town <chumpytown at gmail.com> writes:> Hello, I'm relatively new to R. I've read the intro guide, but I > can't quite figure out how to: > > I have a function: > > Jcost <- function (theta, in, out) { > a <- output - input %*% theta > 1/2 * t(a) %*% a > } > > where > "theta" is a 2x1 matrix > "in" is a 20x2 matrix > "out" is a 20x1 matrix > return value is a scaler > > This works well when I only want to compute given 1 theta matrix. How > do I compute several (say N) 2x1 theta matrices and get back N scaler > values? > > Thanks!Two tricks: (A) You can't do this> matrix(1:10,10,1)-matrix(1:20,10,2)Error in matrix(1:10, 10, 1) - matrix(1:20, 10, 2) : non-conformable arrays but you can do this (vector recycling)> as.vector(matrix(1:10,10,1))-matrix(1:20,10,2)[,1] [,2] [1,] 0 -10 [2,] 0 -10 [3,] 0 -10 [4,] 0 -10 [5,] 0 -10 [6,] 0 -10 [7,] 0 -10 [8,] 0 -10 [9,] 0 -10 [10,] 0 -10 so if you put your theta vectors into a 2xN matrix you can get a 20xN "a" matrix. (B) If "a" has more than one column, you can use diag(t(a)%*%a) to get the N scalars that you want, but that computes all the cross-products unnecessarily. It will be much better to use colSums(a^2). -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Gabor Grothendieck
2004-Sep-25 23:28 UTC
[R] making custom function compute/return multiple items
chumpy town <chumpytown <at> gmail.com> writes:
:
: Hello, I'm relatively new to R. I've read the intro guide, but I
: can't quite figure out how to:
:
: I have a function:
:
: Jcost <- function (theta, in, out) {
: a <- output - input %*% theta
: 1/2 * t(a) %*% a
: }
:
: where
: "theta" is a 2x1 matrix
: "in" is a 20x2 matrix
: "out" is a 20x1 matrix
: return value is a scaler
:
: This works well when I only want to compute given 1 theta matrix. How
: do I compute several (say N) 2x1 theta matrices and get back N scaler
: values?
Assuming `theta' is 2xN, apply the formula over the columns, i.e. dimension
2,
of `theta'.
By the way, note that `in', which is used above as a parameter to
`Jcost', is
a reserved word in R and so should be avoided.
Jcost <- function(theta, input, output)
apply(as.matrix(theta), 2, function(theta)
1/2 * crossprod(output - input %*% theta))
Jcost(1:2, matrix(1:40,20), 1:20)
Jcost(3:4, matrix(1:40,20), 1:20)
# the following combines both of the above two lines into one:
Jcost(cbind(1:2, 3:4), matrix(1:40,20), 1:20)
Seemingly Similar Threads
- [PATCH 1/6] drm/nouveau: convert to using is_hdmi and has_audio from display info
- [PATCH v2 0/4] drm/connector: Provide generic support for underscan
- [PATCH 1/2] drm/nouveau/disp/nv50-: force scaler for any non-default LVDS/eDP modes
- [PATCH 1/6] drm/nouveau: convert to using is_hdmi and has_audio from display info
- [PATCH 00/12] drm: reduce drm_detect_monitor_audio/drm_detect_hdmi_monitor/edid_blob_ptr usage