Hi everyone, I would like to do the following. Given matrix m and matrix n, I would like to compute mn[i,,j]= m[i,,j] + n[i,,j] if either of these elements is 0. (In other words, whichever number is nonzero.) Else I want mn[i,,j]=(m[i,,j] + n[i,,j])/2 I need a fast method. Thanks very much for any help. Bill Simpson
Hi,> Given matrix m and matrix n, I would like to compute mn[i,,j]= m[i,,j] > + n[i,,j] if either of these elements is 0. (In other words, whichever > number is nonzero.) > Else I want mn[i,,j]=(m[i,,j] + n[i,,j])/2 > I need a fast method.m <- matrix(c(0,1,2,3,4,0,5,6,0),nrow=3,ncol=3) n <- matrix(c(1,2,0,3,0,4,0,5,6),nrow=3,ncol=3) mn <- ifelse(m==0 | n==0, m+n,(m+n)/2) Hope that helps, -- Julien Barnier Groupe de recherche sur la socialisation ENS-LSH - Lyon, France
On 21/04/2008, at 10:05 PM, William Simpson wrote:> Hi everyone, > > I would like to do the following. > > Given matrix m and matrix n, I would like to compute mn[i,,j]= m[i,,j] > + n[i,,j] if either of these elements is 0. (In other words, whichever > number is nonzero.) > Else I want mn[i,,j]=(m[i,,j] + n[i,,j])/2 > I need a fast method.I don't know what you're doing with those double commas --- if m and n are indeed matrices they make no sense --- but if they should actually be just commas then what you want is mn <- ifelse(m==0 | n==0,m+n,(m+n)/2) cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}