RE: [R] array addition doesn't recycle!

2004-04-01 Thread Robin Hankin
At 01:39 pm -0500 31/03/04, Raubertas, Richard wrote:
Another alternative is to use the underappreciated function
'sweep()':
sweep(A, 1:2, a, +)

I find the following helpful (it's not due to me but I cannot find 
the original poster):


 %.+% - function(a,x){sweep(a , 2:1 , x ,+ )}
 %+.% - function(a,x){sweep(a , 1:2 , x ,+ )}

 A - matrix(1:16,4,4)
 x - 10^(0:3)

 A %+.% x
 [,1] [,2] [,3] [,4]
[1,]26   10   14
[2,]   12   16   20   24
[3,]  103  107  111  115
[4,] 1004 1008 1012 1016
 A %.+% x
 [,1] [,2] [,3] [,4]
[1,]2   15  109 1013
[2,]3   16  110 1014
[3,]4   17  111 1015
[4,]5   18  112 1016



For 3d arrays this generalizes to

 %+..% - function(a,x){sweep(a , 1 , x ,+ )}
 %.+.% - function(a,x){sweep(a , 2 , x ,+ )}
 %..+% - function(a,x){sweep(a , 3 , x ,+ )}
Then if A - array(1:8,rep(2,3)) and x - c(10,100)

A %+..% x et seq give you a consistent method for addition.

--
Robin Hankin
Uncertainty Analyst
Southampton Oceanography Centre
SO14 3ZH
tel +44(0)23-8059-7743
[EMAIL PROTECTED] (edit in obvious way; spam precaution)
__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


RE: [R] array addition doesn't recycle!

2004-03-31 Thread Raubertas, Richard
Another alternative is to use the underappreciated function 
'sweep()':

sweep(A, 1:2, a, +)

Internally this is about the same as your 'A + array(a, c(2,2,2))'.
But it has the advantage that it makes explicit what the 
relationship between the dimensions of 'A' and 'a' is.  I find 
that relying on implicit recycling tends to produce errors that
are hard to trace, and code that is hard to understand six months
later.

Rich Raubertas
Merck  Co.

 -Original Message-
 From: [EMAIL PROTECTED] 
 [mailto:[EMAIL PROTECTED] On Behalf Of Prof 
 Brian Ripley
 Sent: Wednesday, March 31, 2004 10:00 AM
 To: Tamas Papp
 Cc: R-help mailing list
 Subject: Re: [R] array addition doesn't recycle!
 
 
 The recycling rules are documented and this is not amongst them.
 Computer packages do have a tendency to follow their rules 
 rather than 
 read your mind.
 
 I suspect A + as.vector(a) is what you intended.
 
 On Wed, 31 Mar 2004, Tamas Papp wrote:
 
  Hi,
  
  I have noticed the following:
  
   a - array(1:4, c(2, 2))
   A - array(1:4, c(2,2,2))
   A + a
  Error in A + a : non-conformable arrays
  
  It works with a matrix + a vector, why doesn't it work with arrays?
  Am I missing something?
  
  How would you do the above operation efficiently (ie I need to add a
  matrix to each plane of 3-dim array)?  At the moment I am using
  something like
  
  A + array(a, c(2,2,2))
  
  but it doesn't seem that efficient.
 
 Why do you think is `not that efficient'?  Does you have a 
 need to save 
 microseconds?
 
 -- 
 Brian D. Ripley,  [EMAIL PROTECTED]
 Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
 University of Oxford, Tel:  +44 1865 272861 (self)
 1 South Parks Road, +44 1865 272866 (PA)
 Oxford OX1 3TG, UKFax:  +44 1865 272595
 
 __
 [EMAIL PROTECTED] mailing list
 https://www.stat.math.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide! 
 http://www.R-project.org/posting-guide.html
 


__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html