FAQ
Hi all,

I have a matrix
a<-c(2,3,4,Inf)
b<-as.matrix(a)
[,1]
[1,] 2
[2,] 3
[3,] 4
[4,] Inf
range(b, finite=TRUE)[2] (this is the maximum)
[1] 4

There is a pre-def function to extract the location (in terms of rows) of
the value in the matrix.

In my example would be

3 (max is in the third row)

The maximum is in the position (row) 3.

## Search Discussions

•  at Dec 15, 2011 at 11:13 pm ⇧

Le jeudi 15 d?cembre 2011 ? 21:15 +0100, Trying To learn again a ?crit :
Hi all,

I have a matrix
a<-c(2,3,4,Inf)
b<-as.matrix(a)
[,1]
[1,] 2
[2,] 3
[3,] 4
[4,] Inf
range(b, finite=TRUE)[2] (this is the maximum)
[1] 4

There is a pre-def function to extract the location (in terms of rows) of
the value in the matrix.

In my example would be

3 (max is in the third row)

The maximum is in the position (row) 3.
Maybe using this:
row(b)[b == range(b, finite=TRUE)[2]] [1] 3
col(b)[b == range(b, finite=TRUE)[2]]
[1] 1

Not very short, since in you case involving Inf you cannot use
which.max() directly, but it works.

Regards

## Related Discussions

Discussion Navigation
 view thread | post
Discussion Overview
 group r-help categories r posted Dec 15, '11 at 8:15p active Dec 15, '11 at 11:13p posts 2 users 2 website r-project.org irc #r

### 2 users in discussion

Content

People

Support

Translate

site design / logo © 2017 Grokbase