[R] Is there a faster way to do this?

2010-03-25 Thread Márcio Resende

Hi guys, I am still learning R, and not well familiar with all the apply
functions.
I am trying to find faster alternatives to replace the for cycle.
Is there a faster way to do the example below?

nm - 1000
b - matrix (rnorm (5000, 0, 1), nrow = 500, ncol = nm)
a - matrix (0, nm, nm)
for (i in 1 : nm) {
for (j in 1 : nm) {
if ( j == i) {
next }
a[i, j] - t (b [, i]) %*% b[, j]
}
}

thanks

-- 
View this message in context: 
http://n4.nabble.com/Is-there-a-faster-way-to-do-this-tp1691601p1691601.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] Is there a faster way to do this?

2010-03-25 Thread Peter Alspach
Tena koe Marcio

Seems like you are simply multiplying transpose b by b and replacing the 
diagonal with 0.  If this is correct, then use

a - t(b) %*% b
diag(a) - 0

If this is not a correct interpretation of what you are trying to do, could you 
show us with a small reproducible example.

HTH .

Peter Alspach

 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-
 project.org] On Behalf Of Márcio Resende
 Sent: Friday, 26 March 2010 2:15 p.m.
 To: r-help@r-project.org
 Subject: [R] Is there a faster way to do this?
 
 
 Hi guys, I am still learning R, and not well familiar with all the
 apply
 functions.
 I am trying to find faster alternatives to replace the for cycle.
 Is there a faster way to do the example below?
 
 nm - 1000
 b - matrix (rnorm (5000, 0, 1), nrow = 500, ncol = nm)
 a - matrix (0, nm, nm)
 for (i in 1 : nm) {
 for (j in 1 : nm) {
 if ( j == i) {
 next }
 a[i, j] - t (b [, i]) %*% b[, j]
 }
 }
 
 thanks
 
 --
 View this message in context: http://n4.nabble.com/Is-there-a-faster-
 way-to-do-this-tp1691601p1691601.html
 Sent from the R help mailing list archive at Nabble.com.
 
 __
 R-help@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.
__
R-help@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.


Re: [R] Is there a faster way to do this?

2010-03-25 Thread RICHARD M. HEIBERGER
Márcio,

Think matrix!

Do you really want b to be 100 copies of the same numbers?

You are asking for a strange crossproduct with the main diagonal zeroed out.

a2 - crossprod(a)
a2[cbind(1:1000, 1:1000)] - 0
all.equal(a, a2)


Rich

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Is there a faster way to do it?

2009-10-29 Thread Adaikalavan Ramasamy

You might also want to consider using na.string=9 in the scan().



jim holtman wrote:

Here is a faster way of doing the replacement: (provide reproducible
data next time)


x - matrix(sample(6:9, 64, TRUE), 8)
x

 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]87767879
[2,]77867677
[3,]77769667
[4,]99768766
[5,]69988989
[6,]97697867
[7,]79897978
[8,]99699886

x.f - 1:8  # replacement values based on column
x.ind - which(x == 9, arr.ind=TRUE)
x.ind

  row col
 [1,]   4   1
 [2,]   6   1
 [3,]   8   1
 [4,]   4   2
 [5,]   5   2
 [6,]   7   2
 [7,]   8   2
 [8,]   5   3
 [9,]   6   4
[10,]   7   4
[11,]   8   4
[12,]   3   5
[13,]   8   5
[14,]   5   6
[15,]   7   6
[16,]   1   8
[17,]   5   8

x[x.ind] - x.f[x.ind[,'col']]
x

 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]87767878
[2,]77867677
[3,]77765667
[4,]12768766
[5,]62388688
[6,]17647867
[7,]72847678
[8,]12645886


On Wed, Oct 28, 2009 at 12:55 PM, Marcio Resende
mresende...@yahoo.com.br wrote:

#Mdarts is a matrix 2343x788
#frequencia is a vector 2343x1
# 9 in Mdarts[fri,frj] stands for my missing values which i want to replace
by the value in the vector frequencia


Mdarts-t(matrix(scan(C:/GWS/CNB/dartg.txt),ncol=nindT,nrow=nm, byrow=T))
frequencia - matrix(scan(C:/GWS/CNB/freq.txt),ncol=1)
for (fri in 1:nindT){
for (frj in 1:nm){
Mdarts[fri,frj] - if (Mdarts[fri,frj] == 9) frequencia[frj] else
Mdarts[fri,frj]
Mdarts[fri,frj] - Mdarts[fri,frj]/1-(frequencia[frj]^2)
}
}

Is there a faster way to it?
Maybe using any apply function?
Thanks in advance
--
View this message in context: 
http://www.nabble.com/Is-there-a-faster-way-to-do-it--tp26098223p26098223.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.







__
R-help@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.


[R] Is there a faster way to do it?

2009-10-28 Thread Marcio Resende

#Mdarts is a matrix 2343x788
#frequencia is a vector 2343x1
# 9 in Mdarts[fri,frj] stands for my missing values which i want to replace
by the value in the vector frequencia


Mdarts-t(matrix(scan(C:/GWS/CNB/dartg.txt),ncol=nindT,nrow=nm, byrow=T))
frequencia - matrix(scan(C:/GWS/CNB/freq.txt),ncol=1)
for (fri in 1:nindT){
for (frj in 1:nm){
Mdarts[fri,frj] - if (Mdarts[fri,frj] == 9) frequencia[frj] else
Mdarts[fri,frj]
Mdarts[fri,frj] - Mdarts[fri,frj]/1-(frequencia[frj]^2)
}
}

Is there a faster way to it?
Maybe using any apply function?
Thanks in advance
-- 
View this message in context: 
http://www.nabble.com/Is-there-a-faster-way-to-do-it--tp26098223p26098223.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] Is there a faster way to do it?

2009-10-28 Thread jim holtman
Here is a faster way of doing the replacement: (provide reproducible
data next time)

 x - matrix(sample(6:9, 64, TRUE), 8)
 x
 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]87767879
[2,]77867677
[3,]77769667
[4,]99768766
[5,]69988989
[6,]97697867
[7,]79897978
[8,]99699886
 x.f - 1:8  # replacement values based on column
 x.ind - which(x == 9, arr.ind=TRUE)
 x.ind
  row col
 [1,]   4   1
 [2,]   6   1
 [3,]   8   1
 [4,]   4   2
 [5,]   5   2
 [6,]   7   2
 [7,]   8   2
 [8,]   5   3
 [9,]   6   4
[10,]   7   4
[11,]   8   4
[12,]   3   5
[13,]   8   5
[14,]   5   6
[15,]   7   6
[16,]   1   8
[17,]   5   8
 x[x.ind] - x.f[x.ind[,'col']]
 x
 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]87767878
[2,]77867677
[3,]77765667
[4,]12768766
[5,]62388688
[6,]17647867
[7,]72847678
[8,]12645886


On Wed, Oct 28, 2009 at 12:55 PM, Marcio Resende
mresende...@yahoo.com.br wrote:

 #Mdarts is a matrix 2343x788
 #frequencia is a vector 2343x1
 # 9 in Mdarts[fri,frj] stands for my missing values which i want to replace
 by the value in the vector frequencia


 Mdarts-t(matrix(scan(C:/GWS/CNB/dartg.txt),ncol=nindT,nrow=nm, byrow=T))
 frequencia - matrix(scan(C:/GWS/CNB/freq.txt),ncol=1)
 for (fri in 1:nindT){
 for (frj in 1:nm){
 Mdarts[fri,frj] - if (Mdarts[fri,frj] == 9) frequencia[frj] else
 Mdarts[fri,frj]
 Mdarts[fri,frj] - Mdarts[fri,frj]/1-(frequencia[frj]^2)
 }
 }

 Is there a faster way to it?
 Maybe using any apply function?
 Thanks in advance
 --
 View this message in context: 
 http://www.nabble.com/Is-there-a-faster-way-to-do-it--tp26098223p26098223.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@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.




-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

__
R-help@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.