An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110103/2bddf612/attachment.pl>
matrices call a function element-wise
4 messages · zhaoxing731, Jonathan P Daily, Jonathan Christensen +1 more
IIRC, R is perfectly able to call matrices as vectors, so you might be
able to do this:
FT <- function(i) fisher.test(matrix(c(A[i],B[i],C[i],D[i]),2))
E <- sapply(1:1000000, FT)
Though I don't know how much time you will save.
--------------------------------------
Jonathan P. Daily
Technician - USGS Leetown Science Center
11649 Leetown Road
Kearneysville WV, 25430
(304) 724-4480
"Is the room still a room when its empty? Does the room,
the thing itself have purpose? Or do we, what's the word... imbue it."
- Jubal Early, Firefly
r-help-bounces at r-project.org wrote on 01/03/2011 09:57:14 AM:
[image removed]
[R] matrices call a function element-wise
zhaoxing731
to:
R-help
01/03/2011 01:46 PM
Sent by:
r-help-bounces at r-project.org
Hello
I have 4 1000*1000 matrix A,B,C,D. I want to use the corresponding
element of the 4 matrices. Using the "for loop" as follow:
E<-o
for (i in 1:1000)
{for (j in 1:1000)
{
E<-fisher.test(matrix(c(A[i][j],B[i][j],C[i][j],D[i][j]),
2))#call fisher.test for every element
}
}
It is so time-consuming
Need vectorization
Yours sincerely
ZhaoXing
Department of Health Statistics
West China School of Public Health
Sichuan University
No.17 Section 3, South Renmin Road
Chengdu, Sichuan 610041
P.R.China
[[alternative HTML version deleted]]
__________________________________________________ 8O?lW"2aQE;"3,4sH]A?Cb7QSJOd? ______________________________________________ R-help at r-project.org 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.
Hi, I would recommend reformatting the data as a 2x2x1000 array and using apply. Jonathan
On Mon, Jan 3, 2011 at 7:57 AM, zhaoxing731 <zhaoxing731 at yahoo.com.cn> wrote:
Hello
I have 4 1000*1000 matrix A,B,C,D. I want to use the corresponding element of the 4 matrices. Using the "for loop" as follow:
E<-o
for (i in 1:1000)
{for (j in 1:1000)
{
E<-fisher.test(matrix(c(A[i][j],B[i][j],C[i][j],D[i][j]),2))#call fisher.test for every element
}
}
It is so time-consuming
Need vectorization
Yours sincerely
ZhaoXing
Department of Health Statistics
West China School of Public Health
Sichuan University
No.17 Section 3, South Renmin Road
Chengdu, Sichuan 610041
P.R.China
[[alternative HTML version deleted]]
__________________________________________________ 8O?lW"2aQE;"3,4sH]A?Cb7QSJOd? ______________________________________________ R-help at r-project.org 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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110103/1e9a0e90/attachment.pl>