[R] Sth better than cycle?
John Fox
jfox at mcmaster.ca
Mon Dec 9 14:41:03 CET 2002
Dear Ott,
At 11:51 AM 12/9/2002 +0100, you wrote:
>I want to calculate expected likelihood over a 2D discrete
>distribution, something like
>
>\sum_k \sum_l p_{kl} L(v_k, v_l)
>
>It is trivial to write a cycle like
>
>for(k in 1:K)
> for(l in 1:L)
> sum <- sum + p[k,l]*L(v[k], v[l])
>
>But is there a more clever way for R?
If the dimensions K and L are the same (isn't that implied by the common
vector v?) then how about something like sum(p * outer(v, v, L)). If there
are two vectors, say v1 and v2, then sum(p * outer(v1, v2, L)).
I hope that this helps,
John
-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox
-----------------------------------------------------
More information about the R-help
mailing list