how to extract specific subscript of a matrix

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

how to extract specific subscript of a matrix

Jinsong Zhao-2
Hi there,

I have a matrix similar as:

M <- matrix(c(2,2,rep(1,12), 2), nrow = 5,byrow = FALSE)

I hope to get the border subscript of the block with value 1. In the
above example, I hope to get:

(3,1), (5,1), (5,2), (4,2), (4,3), (1,3), (1,2), (3,2)

Is there any function can do that? or any implement idea? Thanks!

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 extract specific subscript of a matrix

Jeff Newmiller
M <- matrix(c(2,2,rep(1,12), 2), nrow = 5,byrow = FALSE)
ix <- expand.grid( r = seq.int( nrow( M ) )
                 , c = seq.int( ncol( M ) )
                 )
ix[ 1 == c(M), ]


On June 10, 2020 5:29:10 PM PDT, Jinsong Zhao <[hidden email]> wrote:

>Hi there,
>
>I have a matrix similar as:
>
>M <- matrix(c(2,2,rep(1,12), 2), nrow = 5,byrow = FALSE)
>
>I hope to get the border subscript of the block with value 1. In the
>above example, I hope to get:
>
>(3,1), (5,1), (5,2), (4,2), (4,3), (1,3), (1,2), (3,2)
>
>Is there any function can do that? or any implement idea? Thanks!
>
>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.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[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 extract specific subscript of a matrix

Rasmus Liland-3
On 2020-06-10 18:01 -0700, Jeff Newmiller wrote:
> On June 10, 2020 5:29:10 PM PDT, Jinsong Zhao wrote:
> >
> > (3,1), (5,1), (5,2), (4,2), (4,3), (1,3), (1,2), (3,2)
>
> M <- matrix(c(2,2,rep(1,12), 2), nrow = 5,byrow = FALSE)
> ix <- expand.grid( r = seq.int( nrow( M ) )
>                  , c = seq.int( ncol( M ) )
>                  )
> ix[ 1 == c(M), ]

Dear Jinsong and Jeff,

I thought out this, really similar to Jeff's answer:

        M <- matrix(c(2, 2, rep(1, 12), 2),
                    nrow=5, byrow=FALSE)
       
        points <- expand.grid(1:nrow(M), 1:ncol(M))
        points <- apply(points, 1, paste, collapse=",")
        points <- matrix(paste0("(", points, ")"),
                         nrow=nrow(M))
       
        paste(points[M==1], collapse=", ")

you get

        [1] "(3,1), (4,1), (5,1), (1,2), (2,2), (3,2), (4,2), (5,2), (1,3), (2,3), (3,3), (4,3)"

Best,
Rasmus

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

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: how to extract specific subscript of a matrix

Peter Dalgaard-2
In reply to this post by Jeff Newmiller
For that, it is more straightforward to use

which(M==1, arr.ind=TRUE)

However, the desired output has 8 indices, not 12. I don't see what the desired pattern is...

- pd

> On 11 Jun 2020, at 03:01 , Jeff Newmiller <[hidden email]> wrote:
>
> M <- matrix(c(2,2,rep(1,12), 2), nrow = 5,byrow = FALSE)
> ix <- expand.grid( r = seq.int( nrow( M ) )
>                 , c = seq.int( ncol( M ) )
>                 )
> ix[ 1 == c(M), ]
>
>
> On June 10, 2020 5:29:10 PM PDT, Jinsong Zhao <[hidden email]> wrote:
>> Hi there,
>>
>> I have a matrix similar as:
>>
>> M <- matrix(c(2,2,rep(1,12), 2), nrow = 5,byrow = FALSE)
>>
>> I hope to get the border subscript of the block with value 1. In the
>> above example, I hope to get:
>>
>> (3,1), (5,1), (5,2), (4,2), (4,3), (1,3), (1,2), (3,2)
>>
>> Is there any function can do that? or any implement idea? Thanks!
>>
>> 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.
>
> --
> Sent from my phone. Please excuse my brevity.
>
> ______________________________________________
> [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.

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [hidden email]  Priv: [hidden email]

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