[R] Matrix manipulation

Petr PIKAL petr.pikal at precheza.cz
Mon Apr 16 15:10:44 CEST 2007


r-help-bounces at stat.math.ethz.ch napsal dne 16.04.2007 14:52:55:

> Hi,
> 
> This is a very basic question, but apparently I am too stupid for it.
> 
> I have a large matrix A, and I need to avoid for loops. How could I 
> apply a function f(a,r,c) on each element of A, using the subscript (row 

> and column) of a as the other arguments?

Hi

fff<-function(a,b,c) a*b+c
x<-1:12
dim(x)<-c(3,4)
x
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12

fff(x, col(x), row(x))
     [,1] [,2] [,3] [,4]
[1,]    2    9   22   41
[2,]    4   12   26   46
[3,]    6   15   30   51

works. However from your function description is really tough to 
understand what the function really does so maybe this is not what you 
expected.

Regards
Petr

> 
> Thanks in advance,
> Markku Karhunen
> National Public Health Institute,
> Finland
> 
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list