how to use a matrix as an index to another matrix?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

how to use a matrix as an index to another matrix?

Jinsong Zhao-2
Hi there,

I have two matrices, A and B. The columns of B is the index of the
corresponding columns of A. I hope to rearrange of A by B. A minimal
example is following:

 > set.seed(123)
 > A <- matrix(sample(1:10), nrow = 5)
 > B <- matrix(c(sample(1:5), sample(1:5)), nrow =5, byrow = FALSE)
 > A
      [,1] [,2]
[1,]    3    9
[2,]   10    1
[3,]    2    7
[4,]    8    5
[5,]    6    4
 > B
      [,1] [,2]
[1,]    2    1
[2,]    3    4
[3,]    1    5
[4,]    4    3
[5,]    5    2
 > A[,1] <- A[,1][B[,1]]
 > A[,2] <- A[,2][B[,2]]
 > A
      [,1] [,2]
[1,]   10    9
[2,]    2    5
[3,]    3    4
[4,]    8    7
[5,]    6    1

My question is whether there is any elegant or generalized way to replace:

 > A[,1] <- A[,1][B[,1]]
 > A[,2] <- A[,2][B[,2]]

Thanks in advance.

PS., I know how to do the above thing by loop.

Best,
Jinsong

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: how to use a matrix as an index to another matrix?

Eric Berger
Here is one way
A <- sapply(1:ncol(A), function(i) {A[,i][B[,i]]})


On Fri, Oct 11, 2019 at 12:44 PM Jinsong Zhao <[hidden email]> wrote:

> Hi there,
>
> I have two matrices, A and B. The columns of B is the index of the
> corresponding columns of A. I hope to rearrange of A by B. A minimal
> example is following:
>
>  > set.seed(123)
>  > A <- matrix(sample(1:10), nrow = 5)
>  > B <- matrix(c(sample(1:5), sample(1:5)), nrow =5, byrow = FALSE)
>  > A
>       [,1] [,2]
> [1,]    3    9
> [2,]   10    1
> [3,]    2    7
> [4,]    8    5
> [5,]    6    4
>  > B
>       [,1] [,2]
> [1,]    2    1
> [2,]    3    4
> [3,]    1    5
> [4,]    4    3
> [5,]    5    2
>  > A[,1] <- A[,1][B[,1]]
>  > A[,2] <- A[,2][B[,2]]
>  > A
>       [,1] [,2]
> [1,]   10    9
> [2,]    2    5
> [3,]    3    4
> [4,]    8    7
> [5,]    6    1
>
> My question is whether there is any elegant or generalized way to replace:
>
>  > A[,1] <- A[,1][B[,1]]
>  > A[,2] <- A[,2][B[,2]]
>
> Thanks in advance.
>
> PS., I know how to do the above thing by loop.
>
> Best,
> Jinsong
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: how to use a matrix as an index to another matrix?

Martin Morgan-4
A matrix can be subset by another 2-column matrix, where the first column is the row index and the second column the column index. So

idx = matrix(c(B, col(B)), ncol = 2)
A[] <- A[idx]

Martin Morgan

´╗┐On 10/11/19, 6:31 AM, "R-help on behalf of Eric Berger" <[hidden email] on behalf of [hidden email]> wrote:

    Here is one way
    A <- sapply(1:ncol(A), function(i) {A[,i][B[,i]]})
   
   
    On Fri, Oct 11, 2019 at 12:44 PM Jinsong Zhao <[hidden email]> wrote:
   
    > Hi there,
    >
    > I have two matrices, A and B. The columns of B is the index of the
    > corresponding columns of A. I hope to rearrange of A by B. A minimal
    > example is following:
    >
    >  > set.seed(123)
    >  > A <- matrix(sample(1:10), nrow = 5)
    >  > B <- matrix(c(sample(1:5), sample(1:5)), nrow =5, byrow = FALSE)
    >  > A
    >       [,1] [,2]
    > [1,]    3    9
    > [2,]   10    1
    > [3,]    2    7
    > [4,]    8    5
    > [5,]    6    4
    >  > B
    >       [,1] [,2]
    > [1,]    2    1
    > [2,]    3    4
    > [3,]    1    5
    > [4,]    4    3
    > [5,]    5    2
    >  > A[,1] <- A[,1][B[,1]]
    >  > A[,2] <- A[,2][B[,2]]
    >  > A
    >       [,1] [,2]
    > [1,]   10    9
    > [2,]    2    5
    > [3,]    3    4
    > [4,]    8    7
    > [5,]    6    1
    >
    > My question is whether there is any elegant or generalized way to replace:
    >
    >  > A[,1] <- A[,1][B[,1]]
    >  > A[,2] <- A[,2][B[,2]]
    >
    > Thanks in advance.
    >
    > PS., I know how to do the above thing by loop.
    >
    > Best,
    > Jinsong
    >
    > ______________________________________________
    > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
    > 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.
    >
   
    [[alternative HTML version deleted]]
   
    ______________________________________________
    [hidden email] mailing list -- To UNSUBSCRIBE and more, see
    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.
   
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: how to use a matrix as an index to another matrix?

Linus Chen
In reply to this post by Jinsong Zhao-2
Hi Jinsong,

In such a case I think explicit loop IS the most elegant solution.
for(i in 1:2) A[,i] <- A[,i][B[,i]]

Linus

On Fri, 11 Oct 2019 at 11:44, Jinsong Zhao <[hidden email]> wrote:

>
> Hi there,
>
> I have two matrices, A and B. The columns of B is the index of the
> corresponding columns of A. I hope to rearrange of A by B. A minimal
> example is following:
>
>  > set.seed(123)
>  > A <- matrix(sample(1:10), nrow = 5)
>  > B <- matrix(c(sample(1:5), sample(1:5)), nrow =5, byrow = FALSE)
>  > A
>       [,1] [,2]
> [1,]    3    9
> [2,]   10    1
> [3,]    2    7
> [4,]    8    5
> [5,]    6    4
>  > B
>       [,1] [,2]
> [1,]    2    1
> [2,]    3    4
> [3,]    1    5
> [4,]    4    3
> [5,]    5    2
>  > A[,1] <- A[,1][B[,1]]
>  > A[,2] <- A[,2][B[,2]]
>  > A
>       [,1] [,2]
> [1,]   10    9
> [2,]    2    5
> [3,]    3    4
> [4,]    8    7
> [5,]    6    1
>
> My question is whether there is any elegant or generalized way to replace:
>
>  > A[,1] <- A[,1][B[,1]]
>  > A[,2] <- A[,2][B[,2]]
>
> Thanks in advance.
>
> PS., I know how to do the above thing by loop.
>
> Best,
> Jinsong
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: how to use a matrix as an index to another matrix?

Bert Gunter-2
No loops necessary. Use array indexing (see ?"[", of course -- the section
on matrices and arrays)

set.seed(123)
 A <- matrix(sample(1:10), nrow = 5)
 B <- matrix(c(sample(1:5), sample(1:5)), nrow =5, byrow = FALSE)
## The following could be a 1-liner, but I broke it out for clarity.
 ix <- cbind(as.vector(B), rep(1:2, e=5))
 ix
matrix(A[ix], ncol =2)

Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Mon, Oct 28, 2019 at 1:48 PM Linus Chen <[hidden email]> wrote:

> Hi Jinsong,
>
> In such a case I think explicit loop IS the most elegant solution.
> for(i in 1:2) A[,i] <- A[,i][B[,i]]
>
> Linus
>
> On Fri, 11 Oct 2019 at 11:44, Jinsong Zhao <[hidden email]> wrote:
> >
> > Hi there,
> >
> > I have two matrices, A and B. The columns of B is the index of the
> > corresponding columns of A. I hope to rearrange of A by B. A minimal
> > example is following:
> >
> >  > set.seed(123)
> >  > A <- matrix(sample(1:10), nrow = 5)
> >  > B <- matrix(c(sample(1:5), sample(1:5)), nrow =5, byrow = FALSE)
> >  > A
> >       [,1] [,2]
> > [1,]    3    9
> > [2,]   10    1
> > [3,]    2    7
> > [4,]    8    5
> > [5,]    6    4
> >  > B
> >       [,1] [,2]
> > [1,]    2    1
> > [2,]    3    4
> > [3,]    1    5
> > [4,]    4    3
> > [5,]    5    2
> >  > A[,1] <- A[,1][B[,1]]
> >  > A[,2] <- A[,2][B[,2]]
> >  > A
> >       [,1] [,2]
> > [1,]   10    9
> > [2,]    2    5
> > [3,]    3    4
> > [4,]    8    7
> > [5,]    6    1
> >
> > My question is whether there is any elegant or generalized way to
> replace:
> >
> >  > A[,1] <- A[,1][B[,1]]
> >  > A[,2] <- A[,2][B[,2]]
> >
> > Thanks in advance.
> >
> > PS., I know how to do the above thing by loop.
> >
> > Best,
> > Jinsong
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.