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

From: jim holtman <jholtman_at_gmail.com>
Date: Sun, 02 Mar 2008 07:18:50 -0500

> 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,]    1    1    1    2    1
[2,]    1    0    1    1    0
[3,]    0    0    1    0    2
[4,]    0    1    2    0    0
[5,]    1    2    1    2    2

> B
[,1] [,2] [,3] [,4] [,5]
[1,]    1    6   11   16   21
[2,]    2    7   12   17   22
[3,]    3    8   13   18   23
```

[4,] 4 9 14 19 24
[5,] 5 10 15 20 25
> C

[,1] [,2] [,3] [,4] [,5]

```[1,]    1    6   11   16   21
[2,]    2    0   12   17    0
[3,]    0    0   13    0   23
[4,]    0    9   14    0    0
[5,]    5   10   15   20   25
```

>

On Sun, Mar 2, 2008 at 7:11 AM, Diogo André Alagador <mcnda839_at_mncn.csic.es> wrote:
> To all,
>
>
>
> I am undergoing an analysis involving big matrices of about 30000x200 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".
>
>
>
>
>
>
>
> Biodiversity & Global Change Lab, Museo Nacional de Ciencias Naturales,
> Forest Research Centre, Instituto Superior de Agronomia, Universidade
> Técnica de Lisboa, Lisboa, Portugal
>
>
> [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> 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_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
and provide commented, minimal, self-contained, reproducible code.
```
Received on Sun 02 Mar 2008 - 12:20:58 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 02 Mar 2008 - 14:00:18 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.