Quantcast

matrix element position: from length to dim

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

matrix element position: from length to dim

anaraster
How can I pass from position in length inside a matrix to position in dim ?


a=matrix(c(1:999),nrow=9)

which(a==87)    #position in length 1:length(a)
87

which(a==87,arr.ind=TRUE)   #position in dim
     row col
[1,]   6  10

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: matrix element position: from length to dim

Petr Savicky
On Thu, Feb 02, 2012 at 02:08:37PM +0100, Ana wrote:

> How can I pass from position in length inside a matrix to position in dim ?
>
>
> a=matrix(c(1:999),nrow=9)
>
> which(a==87)    #position in length 1:length(a)
> 87
>
> which(a==87,arr.ind=TRUE)   #position in dim
>      row col
> [1,]   6  10

Hi.

Assume

   d <- dim(a)
   i <- 87

Try the following two approaches.

1.

   x <- rep(FALSE, times=prod(d))
   x[i] <- TRUE
   which(array(x, dim=d), arr.ind=TRUE)

       row col
  [1,]   6  10

2.

  c((i - 1) %% d[1], (i - 1) %/% d[1]) + 1

  [1]  6 10

Hope this helps.

Petr Savicky.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: matrix element position: from length to dim

Michael Weylandt
Also take a look at the arrayInd() function which is what's used by
which() internally for the arr.ind = TRUE case.

Michael

On Thu, Feb 2, 2012 at 8:25 AM, Petr Savicky <[hidden email]> wrote:

> On Thu, Feb 02, 2012 at 02:08:37PM +0100, Ana wrote:
>> How can I pass from position in length inside a matrix to position in dim ?
>>
>>
>> a=matrix(c(1:999),nrow=9)
>>
>> which(a==87)    #position in length 1:length(a)
>> 87
>>
>> which(a==87,arr.ind=TRUE)   #position in dim
>>      row col
>> [1,]   6  10
>
> Hi.
>
> Assume
>
>   d <- dim(a)
>   i <- 87
>
> Try the following two approaches.
>
> 1.
>
>   x <- rep(FALSE, times=prod(d))
>   x[i] <- TRUE
>   which(array(x, dim=d), arr.ind=TRUE)
>
>       row col
>  [1,]   6  10
>
> 2.
>
>  c((i - 1) %% d[1], (i - 1) %/% d[1]) + 1
>
>  [1]  6 10
>
> Hope this helps.
>
> Petr Savicky.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Loading...