[R] Index of item in matrix
Spencer Graves
spencer.graves at pdf.com
Thu Apr 3 21:19:28 CEST 2003
Sundar's solution is better than mine unless the code must also work in
S-Plus.
Spencer Graves
Sundar Dorai-Raj wrote:
> Or more simply:
>
> > set.seed(1)
> > A <- array(rnorm(12), dim=c(3,4))
> > which(round(A) == 1, arr.ind = TRUE)
> row col
> [1,] 2 2
> [2,] 1 4
>
> Though, the original post may need more clarification.
>
> Sundar
>
> Spencer Graves wrote:
>
>> Satisfying what conditions?
>>
>> Does the following example help you get what you want?
>>
>> > set.seed(1)
>> > A <- array(rnorm(12), dim=c(3,4))
>> >
>> > sel <- (round(A)==1)
>> > sel
>> [,1] [,2] [,3] [,4]
>> [1,] FALSE FALSE FALSE TRUE
>> [2,] FALSE TRUE FALSE FALSE
>> [3,] FALSE FALSE FALSE FALSE
>> > cbind(row(A)[sel],col(A)[sel])
>> [,1] [,2]
>> [1,] 2 2
>> [2,] 1 4
>>
>> Spencer Graves
>>
>> John Janmaat wrote:
>>
>>> Hello All,
>>>
>>> Is there a fast way to find the index(row and column) of a point in a
>>> matrix?
>>>
>>> Thanks,
>>>
>>> John.
>>
>>
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>>
>
>
More information about the R-help
mailing list