[R] is there a way to let R do smart matrix-vector operation?
Robin Hankin
r.hankin at noc.soton.ac.uk
Tue Mar 7 09:07:45 CET 2006
One can always use the often-overlooked sweep():
> a <- matrix(2:7,2,3)
> b <- matrix(1:2,2,1)
> sweep(a,1,b)
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 1 3 5
>
best
rksh
On 6 Mar 2006, at 23:10, Michael wrote:
> Hi all,
>
> I want to substract vector B from A's each column... how can R do that
> smartly without a loop?
>
>> A=matrix(c(2:7), 2, 3)
>> A
> [,1] [,2] [,3]
> [1,] 2 4 6
> [2,] 3 5 7
>> B=matrix(c(1, 2), 2, 1)
>> B
> [,1]
> [1,] 1
> [2,] 2
>> A-B
> Error in A - B : non-conformable arrays
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-
> guide.html
--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
tel 023-8059-7743
More information about the R-help
mailing list