Re: [R] an efficient pairwise matrix cell's comparison function

2008-03-02 Thread jim holtman
Does this do what you want?

 A - matrix(sample(0:2, 25, TRUE), ncol=5)
 B - matrix(1:25, ncol=5)
 C - ifelse(A == 0, 0, B)
 A
 [,1] [,2] [,3] [,4] [,5]
[1,]11121
[2,]10110
[3,]00102
[4,]01200
[5,]12122
 B
 [,1] [,2] [,3] [,4] [,5]
[1,]16   11   16   21
[2,]27   12   17   22
[3,]38   13   18   23
[4,]49   14   19   24
[5,]5   10   15   20   25
 C
 [,1] [,2] [,3] [,4] [,5]
[1,]16   11   16   21
[2,]20   12   170
[3,]00   130   23
[4,]09   1400
[5,]5   10   15   20   25



On Sun, Mar 2, 2008 at 7:11 AM, Diogo André Alagador
[EMAIL PROTECTED] wrote:
 To all,



 I am undergoing an analysis involving big matrices of about 3x200 which
 I have to handle in a more efficient way. So I would like some advice to
 build such efficient function to deliver the following result:



 -  starting with 2 matrices of the same dimension (eg. A and B)



   0  0  3  5  6  0  0  5

 A=   0  0  6  4  B=   0  4  3  5

 0  0  5  0  1  0  0  9



 -  the function should deliver a C matrix (same dimension too),
 where at each position C(i,j), compares A and B.

  if A(i,j)=0, than C(i,j)=0,

  if A(i,j)!=0, than C(i,j)=B(i,j)



  6  0  0  5

 C= 0  0  3  5

  0  0  0  0



 Although not an expert I could build a function with 2 cycles (reading
 columns and rows) which is not quick. Maybe you can help me in this
 challenge.



 Much thanks in advance,




 Diogo André Alagador
 Biodiversity  Global Change Lab, Museo Nacional de Ciencias Naturales,
 CSIC, Madrid, España
 Forest Research Centre, Instituto Superior de Agronomia, Universidade
 Técnica de Lisboa, Lisboa, Portugal


[[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.





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

What is the problem 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.


Re: [R] an efficient pairwise matrix cell's comparison function

2008-03-02 Thread Prof Brian Ripley

C - B
C[A==0] - 0

would be somewhat more efficient.

On Sun, 2 Mar 2008, jim holtman wrote:


Does this do what you want?


A - matrix(sample(0:2, 25, TRUE), ncol=5)
B - matrix(1:25, ncol=5)
C - ifelse(A == 0, 0, B)
A

[,1] [,2] [,3] [,4] [,5]
[1,]11121
[2,]10110
[3,]00102
[4,]01200
[5,]12122

B

[,1] [,2] [,3] [,4] [,5]
[1,]16   11   16   21
[2,]27   12   17   22
[3,]38   13   18   23
[4,]49   14   19   24
[5,]5   10   15   20   25

C

[,1] [,2] [,3] [,4] [,5]
[1,]16   11   16   21
[2,]20   12   170
[3,]00   130   23
[4,]09   1400
[5,]5   10   15   20   25





On Sun, Mar 2, 2008 at 7:11 AM, Diogo André Alagador
[EMAIL PROTECTED] wrote:

To all,



I am undergoing an analysis involving big matrices of about 3x200 which
I have to handle in a more efficient way. So I would like some advice to
build such efficient function to deliver the following result:



-  starting with 2 matrices of the same dimension (eg. A and B)



  0  0  3  5  6  0  0  5

A=   0  0  6  4  B=   0  4  3  5

0  0  5  0  1  0  0  9



-  the function should deliver a C matrix (same dimension too),
where at each position C(i,j), compares A and B.

 if A(i,j)=0, than C(i,j)=0,

 if A(i,j)!=0, than C(i,j)=B(i,j)



 6  0  0  5

C= 0  0  3  5

 0  0  0  0



Although not an expert I could build a function with 2 cycles (reading
columns and rows) which is not quick. Maybe you can help me in this
challenge.



Much thanks in advance,




Diogo André Alagador
Biodiversity  Global Change Lab, Museo Nacional de Ciencias Naturales,
CSIC, Madrid, España
Forest Research Centre, Instituto Superior de Agronomia, Universidade
Técnica de Lisboa, Lisboa, Portugal


   [[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.






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

What is the problem 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.



--
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__
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.